Skip to content

Instantly share code, notes, and snippets.

@wowkin2
Created January 22, 2019 22:24
Show Gist options
  • Save wowkin2/2d66c67baa7c1b7d0697aa4d31abacb2 to your computer and use it in GitHub Desktop.
Save wowkin2/2d66c67baa7c1b7d0697aa4d31abacb2 to your computer and use it in GitHub Desktop.
API to check password on HaveIBeenPwned service
import hashlib
import requests
import getpass
def test_pw(byte_string):
hasher = hashlib.sha1()
hasher.update(byte_string)
digest = hasher.hexdigest().upper()
print(f'Hash: {digest[:5]}, {digest[5:]}')
print(f'GET https://api.pwnedpasswords.com/range/{digest[:5]}')
pw_list = requests.get(f'https://api.pwnedpasswords.com/range/{digest[:5]}')
for line in pw_list.text.split('\n'):
info = line.split(':')
if info[0] == digest[5:]:
print(f'Pwned! Seen {int(info[1])} times.')
break
else:
print('Not found')
pw = getpass.getpass()
test_pw(pw.encode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment