-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrength.py
More file actions
161 lines (131 loc) · 4.44 KB
/
strength.py
File metadata and controls
161 lines (131 loc) · 4.44 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import tkinter as tk
import re
import random
import string
import math
# ---------- Password Strength Logic ----------
def password_strength(password):
score = 0
length = len(password)
# Length scoring
if length >= 8:
score += min(40, length * 2) # up to 40 points
# Character variety
if re.search(r'[a-z]', password):
score += 10
if re.search(r'[A-Z]', password):
score += 10
if re.search(r'[0-9]', password):
score += 10
if re.search(r'[^a-zA-Z0-9]', password):
score += 20
# Common patterns penalty
common_patterns = ["password", "1234", "qwerty", "abcd"]
for pat in common_patterns:
if pat in password.lower():
score -= 20
# Clamp score
score = max(0, min(100, score))
# Strength label
if score < 40:
strength = "WEAK ❌"
elif score < 70:
strength = "MEDIUM ⚠️"
else:
strength = "STRONG ✅"
return strength, score
# ---------- Crack Time Estimator ----------
def crack_time(password):
length = len(password)
charset = 0
if re.search(r'[a-z]', password): charset += 26
if re.search(r'[A-Z]', password): charset += 26
if re.search(r'[0-9]', password): charset += 10
if re.search(r'[^a-zA-Z0-9]', password): charset += 32 # approx special chars
if charset == 0:
return "Instant"
combinations = charset ** length
guesses_per_second = 1e9 # 1 billion guesses/sec
seconds = combinations / guesses_per_second
# Convert to human-readable
if seconds < 60:
return f"{int(seconds)} seconds"
elif seconds < 3600:
return f"{int(seconds/60)} minutes"
elif seconds < 86400:
return f"{int(seconds/3600)} hours"
elif seconds < 31536000:
return f"{int(seconds/86400)} days"
else:
return "Years+"
# ---------- Password Generator ----------
def generate_password(length=16):
chars = string.ascii_letters + string.digits + string.punctuation
# Ensure at least one of each type
password = [
random.choice(string.ascii_lowercase),
random.choice(string.ascii_uppercase),
random.choice(string.digits),
random.choice(string.punctuation)
]
password += [random.choice(chars) for _ in range(length - 4)]
random.shuffle(password)
return ''.join(password)
# ---------- GUI ----------
def check_strength():
pwd = entry.get()
strength, score = password_strength(pwd)
time = crack_time(pwd)
lbl_strength.config(text=f"Strength: {strength}")
lbl_score.config(text=f"Score: {score}/100")
lbl_time.config(text=f"Estimated Crack Time: {time}")
def strengthen_input():
pwd = entry.get()
if not pwd:
pwd = generate_password(20)
else:
# Shuffle existing characters
chars = list(pwd)
random.shuffle(chars)
# Add some complexity if missing
if not re.search(r'[0-9]', pwd):
chars.append(random.choice(string.digits))
if not re.search(r'[^a-zA-Z0-9]', pwd):
chars.append(random.choice(string.punctuation))
if not re.search(r'[A-Z]', pwd):
chars.append(random.choice(string.ascii_uppercase))
random.shuffle(chars) # Shuffle again after adding new chars
pwd = "".join(chars)
entry.delete(0, tk.END)
entry.insert(0, pwd)
check_strength()
def copy_clipboard():
root.clipboard_clear()
root.clipboard_append(entry.get())
def toggle_visibility():
if entry.cget('show') == '*':
entry.config(show='')
btn_view.config(text="Hide Password")
else:
entry.config(show='*')
btn_view.config(text="View Password")
root = tk.Tk()
root.title("🔐 Password Strength & Safety Analyzer")
root.geometry("400x300")
entry = tk.Entry(root, width=30, show="*")
entry.pack(pady=5)
btn_view = tk.Button(root, text="View Password", command=toggle_visibility)
btn_view.pack(pady=2)
btn_check = tk.Button(root, text="Check Strength", command=check_strength)
btn_check.pack(pady=5)
lbl_strength = tk.Label(root, text="Strength: ")
lbl_strength.pack()
lbl_score = tk.Label(root, text="Score: ")
lbl_score.pack()
lbl_time = tk.Label(root, text="Estimated Crack Time: ")
lbl_time.pack()
btn_generate = tk.Button(root, text="Strengthen & Shuffle", command=strengthen_input)
btn_generate.pack(pady=10)
btn_copy = tk.Button(root, text="Copy to Clipboard", command=copy_clipboard)
btn_copy.pack(pady=5)
root.mainloop()