#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import requests
import re
import sys
import os
C = {
'header': '\033[95m',
'red': '\033[31m',
'green': '\033[32m',
'cyan': '\033[36m',
'okblue': '\033[94m',
'okcyan': '\033[96m',
'okgreen': '\033[92m',
'warn': '\033[93m',
'fail': '\033[91m',
'end': '\033[0m',
'bold': '\033[1m',
'unbold': '\033[22m',
'underline': '\033[4m',
}
def color(prefix, s):
return "%s%s%s" % (prefix, s, ENDC)
def check_leak(x):
SHA1 = hashlib.sha1(x.encode('utf-8'))
hash_string = SHA1.hexdigest().upper()
prefix = hash_string[0:5]
header = {
'User-Agent': 'password checker'
}
url = "https://api.pwnedpasswords.com/range/{}".format(prefix)
req = requests.get(url, headers=header).content.decode('utf-8')
# split the result twice - each line into key, value
# pairs of hash-postfixes and the usage count.
hashes = dict(t.split(":") for t in req.split('\r\n'))
# add the prefix to the key values (hashes) of the hashes dictionary
hashes = dict((prefix + key, value) for (key, value) in hashes.items())
for item_hash in hashes:
if item_hash == hash_string:
print("{warn}warning{end}: password '{red}{bold}{passwd}{end}'"
" has appeared in {bold}{count}{unbold} data breaches."
#.format(**C))
.format(passwd=x, count=hashes[hash_string], **C))
break
if hash_string != item_hash:
print("{okgreen}success{end}: password '{bold}{passwd}{unbold}'"
" hasn't been leaked.".format(passwd=x, **C))
assert len(sys.argv) == 1
strings = []
for x in sys.stdin:
strings.append(x.strip())
for x in strings:
check_leak(x)
'''
-References-
https://haveibeenpwned.com/API/v3#PwnedPasswords
https://www.troyhunt.com/ive-just-launched-pwned-passwords-version-2/
'''