-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordchecker.py
More file actions
42 lines (36 loc) · 1.37 KB
/
passwordchecker.py
File metadata and controls
42 lines (36 loc) · 1.37 KB
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
41
42
import requests
import hashlib
import sys
import bisect
def request_api_data(query_param):
url = 'https://api.pwnedpasswords.com/range/' + query_param
res = requests.get(url)
code = res.status_code
if code != 200:
raise RuntimeError(f'error fetching resonse: {code}, check your request')
else:
return res
def pwned_api_check(password):
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
head, tail = sha1password[:5], sha1password[5:]
response = request_api_data(head)
return get_password_leak_count(response, tail)
def binary_search(hashes, hash_to_check):
hash_values = [hash_tuple[0] for hash_tuple in hashes]
index = bisect.bisect_left(hash_values, hash_to_check)
if index < len(hashes) and hashes[index][0] == hash_to_check:
return hashes[index][1]
return 0
def get_password_leak_count(hashes, hash_to_check):
hashes = [line.split(':') for line in hashes.text.splitlines()]
hashes.sort()
return binary_search(hashes, hash_to_check)
def main(args):
for password in args:
count = pwned_api_check(password)
if count!=0:
print(f'{password} was found {count} times, consider another password')
else:
print(f'{password} was not found and may be considered safe to use')
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))