|
| 1 | +import tkinter as tk |
| 2 | +import requests |
| 3 | +import hashlib |
| 4 | + |
| 5 | + |
| 6 | +def send_request_to_API(start_char): |
| 7 | + """ |
| 8 | + The function sends request to the API. |
| 9 | + """ |
| 10 | + # first 5 characters of hashed password added to the URL |
| 11 | + url = 'https://api.pwnedpasswords.com/range/' + start_char |
| 12 | + try: |
| 13 | + res = requests.get(url) |
| 14 | + |
| 15 | + # Only status code of 200 returns relevant data |
| 16 | + if res.status_code != 200: |
| 17 | + print('\nError fetching results!!!') |
| 18 | + return 0 |
| 19 | + |
| 20 | + return res |
| 21 | + |
| 22 | + # In case connection was not even established |
| 23 | + except: |
| 24 | + print('\nConnection Error!!!') |
| 25 | + return 0 |
| 26 | + |
| 27 | + |
| 28 | +def get_count(res, suffix): |
| 29 | + """ |
| 30 | + The function helps to obtain relevant information for the given |
| 31 | + hashed password |
| 32 | + """ |
| 33 | + # The data has a ':' delimiter separating the hashed password and its count |
| 34 | + results = (line.split(':') for line in res.text.splitlines()) |
| 35 | + |
| 36 | + for hashed, count in results: |
| 37 | + # Finding match for the last 5 characters of the hashed password |
| 38 | + if hashed == suffix: |
| 39 | + return count |
| 40 | + |
| 41 | + return 0 |
| 42 | + |
| 43 | + |
| 44 | +def password_hashing(password): |
| 45 | + """ |
| 46 | + The function generates the SHA-1 hash of a UTF-8 encoded password given by |
| 47 | + user |
| 48 | + """ |
| 49 | + sha1pass = hashlib.sha1(password.encode('utf -8')).hexdigest().upper() |
| 50 | + |
| 51 | + # Inorder to maintain anonymity while sending password to the API, |
| 52 | + # storing only the partial hash for searching |
| 53 | + head, tail = sha1pass[:5], sha1pass[5:] |
| 54 | + return head, tail |
| 55 | + |
| 56 | + |
| 57 | +def submit_info(): |
| 58 | + """ |
| 59 | + The function stores the user input password and displays the result in a |
| 60 | + pop up window |
| 61 | + """ |
| 62 | + # Returns the string obtained from Entry |
| 63 | + password = pass_var.get() |
| 64 | + |
| 65 | + start, end = password_hashing(password) |
| 66 | + res = send_request_to_API(start) |
| 67 | + |
| 68 | + if res: |
| 69 | + num = get_count(res, end) |
| 70 | + |
| 71 | + if num: |
| 72 | + Text = f'Password found {num} times in the dataset.\n Recommended to change it ASAP!' |
| 73 | + else: |
| 74 | + Text = 'Your password was not found in the dataset. \nYou have a safe password!' |
| 75 | + |
| 76 | + else: |
| 77 | + Text = 'Error fetching results' |
| 78 | + |
| 79 | + # Creating a popup window to display results |
| 80 | + global popup |
| 81 | + popup = tk.Toplevel() |
| 82 | + popup.title("Status") |
| 83 | + popup.geometry("400x100") |
| 84 | + |
| 85 | + tk.Label(popup, text = Text, font = ('DejaVu Serif',11, 'bold')).pack() |
| 86 | + button = tk.Button(popup, text="OK", command=popup.destroy) |
| 87 | + button.place(x = 175, y = 50) |
| 88 | + |
| 89 | + pass_var.set("") |
| 90 | + |
| 91 | + |
| 92 | +def show_call(): |
| 93 | + """ |
| 94 | + Call-back function for show_button to show the hidden password |
| 95 | + """ |
| 96 | + pass_entry = tk.Entry(root, textvariable = pass_var, font = ('Ubuntu',12, 'bold'), show = '', justify='center') |
| 97 | + pass_entry.place(x=100, y=25) |
| 98 | + |
| 99 | + |
| 100 | +def hide_call(): |
| 101 | + """ |
| 102 | + Call-back function for hide_button to hide the password |
| 103 | + """ |
| 104 | + pass_entry = tk.Entry(root, textvariable = pass_var, font = ('Ubuntu',12, 'bold'), show = '*', justify='center') |
| 105 | + pass_entry.place(x=100, y=25) |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + """ |
| 110 | + Generates the main window using tkinter which takes user's password as input |
| 111 | + """ |
| 112 | + global root |
| 113 | + root = tk.Tk() |
| 114 | + root.title("Pwned or Not?") |
| 115 | + root.geometry("400x150") |
| 116 | + |
| 117 | + global pass_var |
| 118 | + pass_var=tk.StringVar() |
| 119 | + |
| 120 | + pass_label = tk.Label(root, text = 'Enter your password:', font = ('Ubuntu',12, 'bold')) |
| 121 | + pass_entry = tk.Entry(root, textvariable = pass_var, font = ('Ubuntu',12, 'bold'), show = '*', justify='center') |
| 122 | + |
| 123 | + show_button = tk.Button(root, text="Show", command=show_call) |
| 124 | + hide_button = tk.Button(root,text="Hide", command=hide_call) |
| 125 | + sub_btn = tk.Button(root,text = 'Submit', font = ('Ubuntu',12, 'bold'), command=submit_info) |
| 126 | + |
| 127 | + pass_label.place(x=100) |
| 128 | + pass_entry.place(x=100, y=25) |
| 129 | + |
| 130 | + show_button.place(x=115, y=55) |
| 131 | + hide_button.place(x=235, y=55) |
| 132 | + |
| 133 | + sub_btn.place(x=160, y=100) |
| 134 | + |
| 135 | + root.mainloop() |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + main() |
| 140 | + |
0 commit comments