Skip to content

Instantly share code, notes, and snippets.

@maxmalysh
Created June 11, 2019 18:16
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 maxmalysh/c630203c0de2b34b46b995884182b5d2 to your computer and use it in GitHub Desktop.
Save maxmalysh/c630203c0de2b34b46b995884182b5d2 to your computer and use it in GitHub Desktop.
Python wrapper for the Have I Been Pwned API
def check_password(password):
"""
Returns how many times password appears in the breach data set.
>>> check_password('qwerty123')
'592110'
>>> check_password('Very$ecurePa$$word')
0
"""
import requests
from hashlib import sha1
hashed = sha1(password.encode()).hexdigest().upper()
prefix = hashed[:5]
r = requests.get('https://api.pwnedpasswords.com/range/' + prefix)
for line in r.text.split('\r\n'):
suffix, count = line.split(':')
if hashed[5:] == suffix:
return count
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment