#!/usr/bin/env python3 import csv import fileinput from functools import lru_cache from hashlib import sha1 from urllib.request import Request, urlopen def get_credentials(): with fileinput.input() as csvfile: for credential in csv.DictReader(csvfile, delimiter=',', quotechar='"'): if credential.get('password'): yield credential def get_hash(password): checksum = sha1(password.encode()).hexdigest().upper() return checksum[:5], checksum[5:] @lru_cache(maxsize=4096) def get_range(prefix): request = Request('https://api.pwnedpasswords.com/range/{}'.format(prefix)) request.add_header('User-Agent', 'tenzers-pwned-passwords-checker (+https://gist.github.com/Tenzer/b8aa3cfa09a7e1396a0661de6bf35633)') response = urlopen(request, timeout=10).read() data = dict() for line in response.decode().splitlines(): split_line = line.split(':') data[split_line[0]] = split_line[1] return data def main(): for credential in get_credentials(): prefix, suffix = get_hash(credential['password']) range_data = get_range(prefix) if range_data.get(suffix): print( '{} occurrences found of the password for "{}"'.format( range_data.get(suffix), credential.get('name'), ) ) if __name__ == '__main__': main()