This script assesses the security of any given password by checking its length, character diversity, and commonality, then provides a strength score along with detailed improvement suggestions if needed. It also explains why a password is strong when it meets all the criteria.
Click to view script…
#!/usr/bin/env python3
import re
import getpass
def evaluate_password_strength(password):
"""
Evaluate the password strength based on five criteria:
- At least 8 characters long
- Contains an uppercase letter
- Contains a lowercase letter
- Contains a digit
- Contains a special character
Additionally, the function checks for commonly used (and easily guessable) passwords.
Returns a tuple containing the strength rating, score, and suggestions for improvement.
"""
score = 0
suggestions = []
# Check for common (easily guessable) passwords
common_passwords = {
"password", "12345", "123456", "qwerty", "abc123",
"111111", "123123", "password1", "letmein", "admin"
}
if password.lower() in common_passwords:
suggestions.append("Your password is among the most commonly used and easily guessable passwords. Please choose a more unique password.")
# Override the score for common passwords
score = 0
# Early return the evaluation since it's a significant security risk.
return "Weak", score, suggestions
# Check for minimum length
if len(password) >= 8:
score += 1
else:
suggestions.append("Make sure your password is at least 8 characters long.")
# Check for uppercase letters
if re.search(r'[A-Z]', password):
score += 1
else:
suggestions.append("Include at least one uppercase letter.")
# Check for lowercase letters
if re.search(r'[a-z]', password):
score += 1
else:
suggestions.append("Include at least one lowercase letter.")
# Check for digits
if re.search(r'\d', password):
score += 1
else:
suggestions.append("Include at least one digit.")
# Check for special characters
if re.search(r'[\W_]', password):
score += 1
else:
suggestions.append("Include at least one special character.")
# Determine strength rating based on the score
if score <= 2:
strength = "Weak"
elif score in [3, 4]:
strength = "Moderate"
else:
strength = "Strong"
return strength, score, suggestions
def main():
print("Password Strength Checker")
# Using getpass to hide input for privacy; replace with input() if needed.
password = getpass.getpass("Enter a password to evaluate: ")
strength, score, suggestions = evaluate_password_strength(password)
print(f"\nPassword Strength: {strength}")
print(f"Score: {score} out of 5")
if score == 5:
print("\nExplanation: This password is strong because it meets all the required criteria: it is at least 8 characters long, includes both uppercase and lowercase letters, contains at least one digit, and has at least one special character.")
elif suggestions:
print("\nSuggestions for improvement:")
for suggestion in suggestions:
print(f" - {suggestion}")
if __name__ == '__main__':
main()