Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
import sys
import hashlib
import requests

if len(sys.argv) != 2:
    print("Usage: {0} [password]".format(sys.argv[0]))
    sys.exit()

password = sys.argv[1]
print("Password: {0}".format(password))

sha1 = hashlib.sha1(password.encode('utf-8'))
sha1_text = sha1.hexdigest().upper()

print("SHA-1: {0}".format(sha1_text))

head = sha1_text[:5]
tail = sha1_text[5:]

print('Head {0}, tail: {1}'.format(head, tail))

url = 'https://api.pwnedpasswords.com/range/' + head

res = requests.get(url)
if res.status_code != 200:
    raise RuntimeError("Could not connect!")

hashmap = {}

for line in res.text.splitlines():
    parts = line.split(":")
    hashmap[parts[0]] = parts[1]

if tail in hashmap:
    print("Leaked {0} times :(".format(hashmap[tail]))
else:
    print("Password is safe! Nice! :)")