Created
June 4, 2018 07:27
-
-
Save simon-engledew/a303a5b74a641b09878284840e4448ae to your computer and use it in GitHub Desktop.
Check if a password has been compromised.
This file contains hidden or 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
import hashlib | |
import getpass | |
from urllib.request import urlopen, Request | |
def check(password): | |
digest = hashlib.sha1(password.encode('utf-8')).hexdigest().upper() | |
head, tail = digest[:5], digest[5:] | |
request = Request( | |
f'https://api.pwnedpasswords.com/range/{head}', | |
headers={'User-Agent': 'check.py'} | |
) | |
with urlopen(request) as response: | |
for line in (line.decode('utf-8').strip() for line in response): | |
if line.startswith(tail): | |
_, count = line.split(':', 1) | |
return int(count) | |
return 0 | |
def main(): | |
count = check(getpass.getpass()) | |
if count: | |
print(f'Seen {count} times') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment