Created
June 11, 2019 18:16
-
-
Save maxmalysh/c630203c0de2b34b46b995884182b5d2 to your computer and use it in GitHub Desktop.
Python wrapper for the Have I Been Pwned API
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
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