Skip to content

Instantly share code, notes, and snippets.

@pedroburon
Created November 20, 2018 23:17
Show Gist options
  • Save pedroburon/f3dd0121a347895df6df6b327cb84500 to your computer and use it in GitHub Desktop.
Save pedroburon/f3dd0121a347895df6df6b327cb84500 to your computer and use it in GitHub Desktop.
# coding: utf-8
import urllib
import urllib2
import json
from hashlib import sha1
def check_email(email, truncate_response=True):
'''
Returns a list of breaches dictionaries with at least Name key.
truncate_response=False if you want extended data of the breaches.
'''
truncate = 'true' if truncate_response else 'false'
url = 'https://haveibeenpwned.com/api/breachedaccount/{account}?truncateResponse={truncate}'.format(
account=urllib.quote(email), truncate=truncate)
request = urllib2.Request(url, headers={ 'User-Agent': 'Python', 'API-Version': 2 })
return json.loads(urllib2.urlopen(request).read())
def check_password(password):
'''
Returns the number of appearences of this password on dataset
'''
hex_digest = sha1(password).hexdigest().upper()
url = 'https://api.pwnedpasswords.com/range/' + hex_digest[:5]
request = urllib2.Request(url, headers={ 'User-Agent': 'Python' })
response = urllib2.urlopen(request)
for line in response.readlines():
line = line.strip()
hashed, count = line.split(':')
if hashed == hex_digest[5:]:
return int(count)
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment