-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-building-a-password-generator.py
49 lines (33 loc) · 1.11 KB
/
07-building-a-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
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Password Generator. Generates a random password based on user-defined constraints.
The password will include a mix of uppercase letters, lowercase letters, digits, and special characters.
"""
import re
import secrets
import string
def generate_password(length=16, nums=1, special_chars=1, uppercase=1, lowercase=1):
letters = string.ascii_letters
digits = string.digits
symbols = string.punctuation
all_characters = letters + digits + symbols
while True:
password = ''
for _ in range(length):
password += 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, password))
for constraint, pattern in constraints
):
break
return password
new_password = generate_password()
print('Generated password:', new_password)
if __name__=='__main__':
generate_password()