-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkMyPass.py
40 lines (34 loc) · 1.28 KB
/
checkMyPass.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
40
import requests
import hashlib
import sys
def pwned_api_check(password):
#converts to hashcode and splits it
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper() #hexdigest creates the hexcode upper as its required by api
first5_char, tail = sha1password[:5], sha1password[5:]
#Gets response from API
response = request_api_data(first5_char)
#Compares response with password
count = check_password_leak(response, tail)
return count
def request_api_data(query_char):
url = 'https://api.pwnedpasswords.com/range/' + query_char
res = requests.get(url)
if res.status_code != 200:
raise RuntimeError(f'Error fetching: {res.status_code}, check API key')
return res
def check_password_leak(hashes, hash_to_check):
hashes = (line.split(":") for line in hashes.text.splitlines()) #splitlines splits the text in each line
for h,count in hashes:
if h == hash_to_check:
return count
return 0
def main(args):
for password in args:
count = pwned_api_check(password)
if count != 0:
print(f"It seems your {password} password has been found {count} times..You should change it")
else:
print(f"Your {password} password has not been found, carry on")
return "done!"
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))