-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecret_password_generator.py
More file actions
44 lines (32 loc) · 1.05 KB
/
secret_password_generator.py
File metadata and controls
44 lines (32 loc) · 1.05 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
import re
import secrets
import string
def generate_secret(length=20, nums=3, special_chars=2, uppercase=4, lowercase=4):
# Define the possible characters for the password
letters = string.ascii_letters
digits = string.digits
symbols = string.punctuation
# Combine all characters
all_characters = letters + digits + symbols
# Generate password
while True:
secret = ''
for _ in range(length):
secret += secrets.choice(all_characters)
constraints = [
(nums, r'\d'),
(special_chars, fr'[{symbols}]'),
(uppercase, r'[A-Z]'),
(lowercase, r'[a-z]')
]
# Check constraints
if all(
constraint <= len(re.findall(pattern, secret))
for constraint, pattern in constraints
):
break
return secret
# Run this script as a main program, not an imported module
if __name__ == '__main__':
new_secret = generate_secret()
print('New Secret Password:', new_secret)