Last active
March 23, 2019 18:20
-
-
Save meooow25/a3d7c87989ad15f75a170c429454a58e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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