-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword Generator.py
More file actions
100 lines (81 loc) · 3.2 KB
/
Password Generator.py
File metadata and controls
100 lines (81 loc) · 3.2 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
import random
import string
def generate_password(length, use_upper, use_lower, use_digits, use_symbols):
"""
Generate a random password based on user options.
Args:
length (int): Length of the password.
use_upper (bool): Include uppercase letters.
use_lower (bool): Include lowercase letters.
use_digits (bool): Include digits.
use_symbols (bool): Include symbols.
Returns:
str: The generated password.
"""
# Define character sets
characters = ''
if use_lower:
characters += string.ascii_lowercase
if use_upper:
characters += string.ascii_uppercase
if use_digits:
characters += string.digits
if use_symbols:
characters += string.punctuation # !@#$%^&* etc.
if not characters:
print("Error: At least one character type must be selected!")
return None
# Generate password
password = ''.join(random.choice(characters) for _ in range(length))
return password
def check_strength(password):
"""
Basic strength check: Avoids common weak words and ensures length.
Args:
password (str): The password to check.
Returns:
str: Feedback on strength.
"""
common_weak_words = {"password", "123456", "qwerty", "abc", "letmein"}
if len(password) < 8:
return "Weak: Too short (aim for 12+ characters)."
if any(word in password.lower() for word in common_weak_words):
return "Weak: Contains a common word. Try again."
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_symbol = any(c in string.punctuation for c in password)
score = sum([has_upper, has_lower, has_digit, has_symbol])
if score >= 3:
return "Strong: Good mix of characters!"
elif score >= 2:
return "Medium: Could be stronger—add more variety."
else:
return "Weak: Needs uppercase, numbers, or symbols."
def main():
print("=== Secure Password Generator ===")
# Get user inputs
try:
length = int(input("Enter password length (min 8): "))
if length < 8:
print("Using minimum length of 8.")
length = 8
except ValueError:
print("Invalid length! Using default 12.")
length = 12
use_upper = input("Include uppercase letters? (y/n): ").lower() == 'y'
use_lower = input("Include lowercase letters? (y/n): ").lower() == 'y'
use_digits = input("Include numbers? (y/n): ").lower() == 'y'
use_symbols = input("Include symbols? (y/n): ").lower() == 'y'
# Generate password
password = generate_password(length, use_upper, use_lower, use_digits, use_symbols)
if password:
print(f"\nGenerated Password: {password}")
strength = check_strength(password)
print(f"Strength: {strength}")
# Ask to generate another
again = input("\nGenerate another? (y/n): ").lower()
if again == 'y':
main() # Recursive call for simplicity
if __name__ == "__main__":
main()