Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created June 4, 2018 07:27
Show Gist options
  • Save simon-engledew/a303a5b74a641b09878284840e4448ae to your computer and use it in GitHub Desktop.
Save simon-engledew/a303a5b74a641b09878284840e4448ae to your computer and use it in GitHub Desktop.
Check if a password has been compromised.
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