-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordGenerator.py
82 lines (68 loc) · 2.48 KB
/
PasswordGenerator.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
72
73
74
75
76
77
78
79
80
81
82
import random
import string
def generate_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
def password_strength(password):
length = len(password)
strength = 0
# Add points for password length
if length >= 8:
strength += 1
if length >= 12:
strength += 2
if length >= 16:
strength += 3
# Add points for different types of characters
categories = {
'lower': any(char.islower() for char in password),
'upper': any(char.isupper() for char in password),
'digit': any(char.isdigit() for char in password),
'special': any(char in string.punctuation for char in password)
}
strength += sum(categories.values())
# Penalize common patterns or dictionary words
common_patterns = [
'123', 'abc', 'password', 'qwerty', 'letmein', 'trustno1'
]
if any(pattern in password.lower() for pattern in common_patterns):
strength -= 1
# Penalize repeating characters
if any(password.count(char) > 1 for char in password):
strength -= 1
#Add points for random characters
if any(password.count(char) == 1 for char in password):
strength += 1
# Add points for complete words
if any(password.count(char) > 2 for char in password):
strength -= 1
# Interpret the strength of the password
if strength == 0:
return "Very Weak"
elif 0 < strength < 5:
return "Weak"
elif 5 <= strength < 7:
return "Moderate"
elif 7 <= strength < 9:
return "Strong"
elif strength >= 9:
return "Very Strong"
else:
return strength
def main():
try:
length = int(input("Enter the length of the password: "))
if length <= 0:
print("Please enter a positive number.")
return
password = generate_password(length)
#added print statements to display the password and its strength
print("Generated Password:", password)
print("Password Strength:", password_strength(password))
except ValueError:
print("Invalid input. Please enter a valid number.")
if(input("Do you want to generate another password? (y/n): ").lower() == 'y'):
main()
if __name__ == "__main__":
main()