-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_password_generator.py
36 lines (30 loc) · 1.09 KB
/
random_password_generator.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
import secrets
import string
def generate_password(length: int = 12) -> str:
if length < 12:
length = 12
# Define the character sets
digits = string.digits
uppercase = string.ascii_uppercase
lowercase = string.ascii_lowercase
symbols = '!#%*+,-.;=?_' #string.punctuation
# Ensure at least one character from each set is included
password = [
secrets.choice(digits),
secrets.choice(digits),
secrets.choice(uppercase),
secrets.choice(lowercase),
secrets.choice(symbols),
]
# Fill the rest of the password length with randomly selected characters from all sets
for _ in range(length - len(password)):
password.append(secrets.choice(digits + uppercase + lowercase + symbols))
# Shuffle the password to ensure it's not predictable
secrets.SystemRandom().shuffle(password)
# Join the characters to form the final password string
password = ''.join(password)
return password
if __name__ == '__main__':
for i in range(20):
pw = generate_password()
print(pw)