-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerator.py
More file actions
44 lines (34 loc) · 1.3 KB
/
generator.py
File metadata and controls
44 lines (34 loc) · 1.3 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
"""
Password generation utilities.
Provides secure password generation with customizable length and character sets.
"""
import secrets
import string
def generate_password(length: int = 16) -> str:
"""
Generate a cryptographically secure random password.
Args:
length: Desired password length (default: 16, min: 8, max: 128)
Returns:
Generated password string
Raises:
ValueError: If length is out of valid range
"""
if length < 8:
raise ValueError("Password length must be at least 8 characters")
if length > 128:
raise ValueError("Password length must be at most 128 characters")
# Use a comprehensive character set
alphabet = string.ascii_letters + string.digits + "!@#$%^&*()_+-=[]{}|;:,.<>?"
# Ensure password contains at least one character from each category
password = [
secrets.choice(string.ascii_lowercase),
secrets.choice(string.ascii_uppercase),
secrets.choice(string.digits),
secrets.choice("!@#$%^&*()_+-=[]{}|;:,.<>?"),
]
# Fill the rest randomly
password.extend(secrets.choice(alphabet) for _ in range(length - 4))
# Shuffle to avoid predictable pattern
secrets.SystemRandom().shuffle(password)
return "".join(password)