-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.py
More file actions
88 lines (70 loc) · 2.99 KB
/
password_generator.py
File metadata and controls
88 lines (70 loc) · 2.99 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
import string
import secrets
# --- 1. Core Generator Function ---
def generate_password(length, use_upper=True, use_numbers=True, use_symbols=True):
"""
Generates a cryptographically secure password based on dynamic user constraints.
"""
# Start with lowercase letters as the base
pool = string.ascii_lowercase
password_chars = []
# Dynamically build the character pool and guarantee at least one of each requested type
if use_upper:
pool += string.ascii_uppercase
password_chars.append(secrets.choice(string.ascii_uppercase))
if use_numbers:
pool += string.digits
password_chars.append(secrets.choice(string.digits))
if use_symbols:
pool += string.punctuation
password_chars.append(secrets.choice(string.punctuation))
# Fill the remaining length of the password from the combined pool
remaining_length = length - len(password_chars)
for _ in range(remaining_length):
password_chars.append(secrets.choice(pool))
# Shuffle the list so the guaranteed characters aren't always at the beginning
# secrets.SystemRandom() is the secure version of random.shuffle
secure_rng = secrets.SystemRandom()
secure_rng.shuffle(password_chars)
return "".join(password_chars)
# --- 2. Interactive Application Loop ---
def main():
print("\n" + "="*50)
print("🔐 CRYPTOGRAPHICALLY SECURE PASSWORD GENERATOR 🔐")
print("="*50)
print("Type 'quit' at any prompt to exit.\n")
while True:
# 1. Get dynamic length with error handling
user_input = input("\nEnter desired password length (minimum 8): ")
if user_input.lower() == 'quit':
break
try:
length = int(user_input)
if length < 8:
print("⚠️ For security, passwords should be at least 8 characters long.")
continue
except ValueError:
print("❌ Invalid input. Please enter a whole number.")
continue
# 2. Get dynamic constraints
print("\nCustomize your password complexity (Y/N):")
ans_upper = input("Include UPPERCASE letters? ").lower()
if ans_upper == 'quit': break
use_upper = ans_upper in ['y', 'yes', '']
ans_nums = input("Include NUMBERS? ").lower()
if ans_nums == 'quit': break
use_numbers = ans_nums in ['y', 'yes', '']
ans_sym = input("Include SYMBOLS (!@#$)? ").lower()
if ans_sym == 'quit': break
use_symbols = ans_sym in ['y', 'yes', '']
# 3. Generate and display
print("\n" + "-"*40)
print("GENERATING...")
try:
final_password = generate_password(length, use_upper, use_numbers, use_symbols)
print(f"✅ Your Secure Password: {final_password}")
except Exception as e:
print(f"❌ An error occurred: {e}")
print("-" * 40 + "\n")
if __name__ == "__main__":
main()