-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkmypass.py
56 lines (47 loc) · 1.7 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import requests
import hashlib
import sys
def request_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 the api and try again')
return res
def my_pass(hashes, to_check):
hashes = (line.split(':') for line in hashes.text.splitlines())
for hash, count in hashes:
if hash == to_check:
return count
return 0
def pwned_api_check(password):
sha1pass = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
first5, tail = sha1pass[:5], sha1pass[5:]
response = request_data(first5)
return my_pass(response, tail)
def main(args):
pass_count = pwned_api_check(args)
if not pass_count:
print(f'Good news "{args}" wasn\'t found.')
else:
print(f'{args} has been used {pass_count} times, you should change it')
if __name__ == '__main__':
if len(sys.argv) < 2:
check = input('Enter the password you want to check: ')
c = pwned_api_check(check)
if c:
print(f'{check} has been used {c} times, you should change it')
else:
print(f'Good news "{check}" wasn\'t found.')
else:
fileName = sys.argv[1]
if '.txt' in fileName:
with open(fileName) as file:
passwords = file.readlines()
else:
with open(fileName + '.txt') as file:
passwords = file.readlines()
for password in passwords:
if password[-1] == '\n':
main(password[:-1])
else:
main(password)