-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (58 loc) · 2.11 KB
/
main.py
File metadata and controls
73 lines (58 loc) · 2.11 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
import random
import string
import math
WORDS = [
"sunrise", "planet", "echo", "quantum", "delta", "fusion", "matrix", "shadow",
"nebula", "crystal", "titanium", "galaxy", "storm", "velocity", "breeze",
"cosmos", "signal", "flare", "aurora", "nova", "spark", "horizon", "lunar"
]
def generate_password(num_words=4, include_numbers=True, include_symbols=True):
chosen = random.sample(WORDS, num_words)
password = "-".join(chosen)
if include_numbers:
password += str(random.randint(10, 99))
if include_symbols:
password += random.choice("!@#$%^&*?")
return password
# ---------- Password Strength ----------
def estimate_entropy(password: str) -> float:
pool = 0
if any(c.islower() for c in password): pool += 26
if any(c.isupper() for c in password): pool += 26
if any(c.isdigit() for c in password): pool += 10
if any(c in string.punctuation for c in password): pool += 32
if "-" in password: pool += 1 # account for the dash separator
if pool == 0:
pool = len(set(password)) or 2
entropy = len(password) * math.log2(pool)
return round(entropy, 2)
def strength_label(entropy_bits: float) -> str:
if entropy_bits < 28:
return "Very Weak"
elif entropy_bits < 36:
return "Weak"
elif entropy_bits < 60:
return "Reasonable"
elif entropy_bits < 128:
return "Strong"
else:
return "Very Strong"
# ---------- Main ----------
def main():
print("Password Generator")
try:
num_words = int(input("Enter the number of words: "))
except ValueError:
print("Enter a valid number.")
return
include_numbers = input("Include numbers? (y/n): ").lower() == "y"
include_symbols = input("Include symbols? (y/n): ").lower() == "y"
password = generate_password(num_words, include_numbers, include_symbols)
print("\nGenerated Password:", password)
# Show strength
entropy = estimate_entropy(password)
label = strength_label(entropy)
print(f"Estimated Entropy: {entropy} bits")
print(f"Password Strength: {label}")
if __name__ == "__main__":
main()