Skip to content

Commit 4f90e58

Browse files
authored
Update password_generator.py
Check password strength
1 parent 9354d09 commit 4f90e58

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

GUI/Password Generator/password_generator.py

+71
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
from tkinter import *
2+
from tkinter.ttk import *
13
import tkinter as tk
4+
from PIL import Image, ImageTk
25
import random
36
import string
47
import pyperclip
8+
import re
9+
10+
crack_speed = 400000000000
11+
format = {'Uppercase characters': 0,
12+
'Lowercase characters': 0,
13+
'Special characters': 0,
14+
'Numbers': 0}
15+
16+
entropies = {'Uppercase characters': 26,
17+
'Lowercase characters': 26,
18+
'Special characters': 33,
19+
'Numbers': 10}
520

621
# Define the main function for generating the password
722
def generate_password():
@@ -10,6 +25,8 @@ def generate_password():
1025
password = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=int(password_length)))
1126
password_entry.delete(0, tk.END)
1227
password_entry.insert(0, password)
28+
password_ckeck = password_strength(password)
29+
passwordStrength.config(text=password_ckeck)
1330
else:
1431
password_entry.delete(0, tk.END)
1532
password_entry.insert(0, "Invalid password length")
@@ -19,8 +36,59 @@ def copy_password():
1936
password = password_entry.get()
2037
pyperclip.copy(password)
2138

39+
#Define the function to ckeck the password strength
40+
def password_strength(password):
41+
password_len = len(password)
42+
if(len(password) < 7):
43+
strength = "very weak"
44+
else:
45+
if( len(password) < 9 ):
46+
strength = "weak"
47+
else:
48+
if(len(password) < 11):
49+
strength = "good"
50+
else:
51+
strength = "strong"
52+
for char in password:
53+
if re.match("[0-9]", char):
54+
format["Numbers"] += 1
55+
elif re.match("[a-z]", char):
56+
format["Lowercase characters"] += 1
57+
elif re.match("[A-Z]", char):
58+
format["Uppercase characters"] += 1
59+
else:
60+
format["Special characters"] += 1
61+
entropy = 0
62+
for frm in format.keys():
63+
if format[frm] > 0:
64+
entropy += entropies[frm]
65+
66+
time_ = "hours"
67+
cracked = ((entropy**password_len) / crack_speed) / 3600 # Hours in seconds
68+
69+
if cracked > 24:
70+
cracked = cracked / 24
71+
time_ = "days"
72+
73+
if cracked > 365:
74+
cracked = cracked / 365
75+
time_ = "years"
76+
77+
if time_ == "years" and cracked > 100:
78+
cracked = cracked / 100
79+
time_ = "centuries"
80+
81+
if time_ == "centuries" and cracked > 1000:
82+
cracked = cracked / 1000
83+
time_ = "millennia"
84+
cracked = "\nTime to crack password: {:,.2f} {}".format(cracked, time_)
85+
text = strength + cracked
86+
return text
87+
88+
2289
# Create the main window
2390
root = tk.Tk()
91+
root.geometry("350x350")
2492
root.title("Password Generator")
2593

2694
# Create the password length label and entry widget
@@ -39,6 +107,9 @@ def copy_password():
39107
password_entry = tk.Entry(root, width=30)
40108
password_entry.pack(pady=10)
41109

110+
passwordStrength = tk.Label(root, text = "")
111+
passwordStrength.pack()
112+
42113
# Create the "Copy Password" button
43114
copy_button = tk.Button(root, text="Copy Password", command=copy_password)
44115
copy_button.pack(pady=10)

0 commit comments

Comments
 (0)