-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_complexity_checker.py
64 lines (52 loc) · 2 KB
/
password_complexity_checker.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
import re
def check_password_strength(password):
length = len(password)
has_upper = any(c.isupper() for c in password)
has_lower = any(c.islower() for c in password)
has_digit = any(c.isdigit() for c in password)
has_special = bool(re.search(r'[!@#$%^&*(),.?":{}|<>]', password))
score = 0
feedback = []
# Criteria 1: Length
if length >= 12:
score += 1
elif length >= 8:
feedback.append("Consider using a longer password for better security.")
# Criteria 2: Presence of uppercase and lowercase letters
if has_upper and has_lower:
score += 1
else:
feedback.append("Mixing uppercase and lowercase letters enhances security.")
# Criteria 3: Presence of numbers
if has_digit:
score += 1
else:
feedback.append("Including numbers adds to the complexity of the password.")
# Criteria 4: Presence of special characters
if has_special:
score += 1
else:
feedback.append("Special characters provide an extra layer of security.")
# Determine the strength of the password
if score == 4:
return "Strong", "Strong password! Keep it safe."
elif score == 3:
return "Medium", "Medium password. You can improve it by using a longer password or adding more complex characters."
else:
return "Weak", "Weak password. " + " ".join(feedback)
if __name__ == "__main__":
print("Welcome to the Password Strength Checker!")
print("Please enter your password below:")
while True:
password = input("> ")
if password.lower() == 'exit':
print("Exiting...")
break
strength, feedback = check_password_strength(password)
print(feedback)
# End the loop if the password is strong
if strength == "Strong":
print("Password is strong. Exiting...")
break
else:
print("Please try again.")