-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb48.py
More file actions
42 lines (31 loc) · 1.11 KB
/
Copy pathweb48.py
File metadata and controls
42 lines (31 loc) · 1.11 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
import random
import string
def generate_password(length=48, num_digits=6, num_special=6, max_attempts=100):
letters = string.ascii_letters
digits = string.digits
specials = "!@#$%^&*><"
all_chars = letters
if num_special > 0:
all_chars += specials
if num_digits > 0:
all_chars += digits
if length < (num_digits + num_special):
raise ValueError(
"Password length too small or there is not enough space for digits and special characters."
)
for _ in range(max_attempts):
password = []
password += random.choices(digits, k=num_digits)
password += random.choices(specials, k=num_special)
password += random.choices(all_chars, k=length - num_digits - num_special)
random.shuffle(password)
shuffled_password = "".join(password)
return shuffled_password
raise RuntimeError(
"Could not generate a password after the maximum number of attempts."
)
try:
password = generate_password()
print("Password:", password)
except (ValueError, RuntimeError) as e:
print("Error:", e)