-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpasswd.py
71 lines (58 loc) · 2.57 KB
/
checkpasswd.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import hashlib
import sys
import stdiomask
# Comment out this section if you are using the command line check function.
# This will hide your password when you type it in your terminal. No prying eyes!
print('This app will check to see if your password has been pawned.\nYour full password will never be shared with the outside world.')
question = stdiomask.getpass(
prompt='What password would you like to check? : ')
# This sends the password check query
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 the API and try again.')
return res
# This function gets the count of your password pawns
def get_password_leaks_count(hashes, hash_to_check):
hashes = (line.split(':') for line in hashes.text.splitlines())
for h, count in hashes:
if h == hash_to_check:
return count
return 0
# This hashes and splits your password in order to safely share it with the internet
def pwned_api_check(password):
sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
first5_char, tail = sha1password[:5], sha1password[5:]
response = request_api_data(first5_char)
return get_password_leaks_count(response, tail)
# # Uncomment and call this function below if you want to input the password from the command line instead
# # Dont forget to comment the input command at the top if you use this function
# def main_command(args):
# for password in args:
# count = pwned_api_check(password)
# first_lettrs = password[:5]
# if count:
# print(
# f'{first_lettrs}****** was found {count} times... Find a better password!!')
# else:
# print(f'{first_lettrs}****** was NOT found. Good job! Use it.')
# return 'Done!'
# This is the function that does the checking work and prints the information returned
def main(password):
count = pwned_api_check(password)
first_lettrs = password[:5]
if count:
print(
f'{first_lettrs}****** was found {count} times... Find a better password!!')
else:
print(f'{first_lettrs}****** was NOT found. Good job! Use it.')
return 'Check Complete.'
# Input string call. Be sure to comment this command if using the one below.
if __name__ == "__main__":
sys.exit(main(str(question)))
# Command line call. Uncomment and comment the code above to use.
# if __name__ == "__main__":
# main_command(sys.argv[1:])