Skip to content

Instantly share code, notes, and snippets.

@meooow25
Last active March 23, 2019 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meooow25/a3d7c87989ad15f75a170c429454a58e to your computer and use it in GitHub Desktop.
Save meooow25/a3d7c87989ad15f75a170c429454a58e to your computer and use it in GitHub Desktop.
"""
Checks if a password is pwned using the https://haveibeenpwned.com/ API
Usage: python passcheck.py password
"""
import argparse
import hashlib
import urllib.request
URL = 'https://api.pwnedpasswords.com/range/'
def main(password):
hashobj = hashlib.sha1(password.encode('utf-8'))
hexdigest = hashobj.hexdigest().upper()
print('SHA-1 hash: {}'.format(hexdigest,))
resp = urllib.request.urlopen(URL + hexdigest[:5]).read().decode()
matches = dict(match.split(':') for match in resp.splitlines())
count = matches.get(hexdigest[5:])
if count is None:
print('Good news — no pwnage found!')
else:
print('Oh no — pwned! This password has been seen {:,} times before'.format(int(count)))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('password')
args = parser.parse_args()
main(args.password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment