-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.py
More file actions
84 lines (71 loc) · 2.21 KB
/
Copy pathpassword_generator.py
File metadata and controls
84 lines (71 loc) · 2.21 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import secrets
import string
lower_letters = string.ascii_lowercase
upper_letters = string.ascii_uppercase
numbers = string.digits
specials = string.punctuation
def main():
length = get_length()
global charset
charset = get_charset()
password = get_password(length, charset)
print(f"=" * 50)
print(f"Your password is: {password}")
print(f"=" * 50)
def get_length():
while True:
try:
length = int(input("Length of password (Atleast four): "))
if length <= 3:
raise ValueError
return length
except ValueError:
print("Invalid length!")
continue
def validate_yes_no(prompt):
while True:
try:
result = input(prompt).strip().lower()
if result not in ['yes', 'y', 'no', 'n']:
raise ValueError
return result
except ValueError:
print("Invalid Input!")
continue
def get_charset():
charset = lower_letters
want_upper = validate_yes_no("Include upper case letters? (y/n): ")
want_number = validate_yes_no("Include numbers? (y/n): ")
want_special = validate_yes_no("Include special characters? (y/n): ")
if want_upper in ['yes', 'y']:
charset += upper_letters
if want_number in ['yes', 'y']:
charset += numbers
if want_special in ['yes', 'y']:
charset += specials
return charset
def get_count():
count = 0
default_pwd = ""
if upper_letters in charset:
default_pwd += secrets.choice(upper_letters)
count += 1
if numbers in charset:
default_pwd += secrets.choice(numbers)
count += 1
if specials in charset:
default_pwd += secrets.choice(specials)
count += 1
if lower_letters in charset:
default_pwd += secrets.choice(lower_letters)
count += 1
return [default_pwd, count]
def get_password(n, charset):
ls = get_count()
pwd = ls[0]
for _ in range(n-int(ls[1])):
char = secrets.choice(charset)
pwd += char
return pwd
if __name__ == '__main__':
main()