-
Notifications
You must be signed in to change notification settings - Fork 7
/
random password.py
90 lines (76 loc) · 2.41 KB
/
random password.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
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
85
86
87
88
89
90
import random
import string
import argparse
parser = argparse.ArgumentParser(description="Password generator")
parser.add_argument(
"--no-digits",
help="generate password without digits",
action="store_true"
)
parser.add_argument(
"--no-special-chars",
help="generate password without special characters",
action="store_true",
)
parser.add_argument(
"length", nargs="?", type=int, help="length of the password", default=None
)
arguments = parser.parse_args()
use_arguments = True if (arguments.length or
arguments.no_digits or
arguments.no_special_chars) else False
use_arguments_length = True if arguments.length else False
# Process whether or not to use digits
if use_arguments:
use_digits = not arguments.no_digits
else:
while True:
use_digits_answer = input("Use digits [y]/n: ")
if use_digits_answer.lower() not in ["", "y", "n"]:
print("Invalid answer")
continue
break
if use_digits_answer.lower() == "n":
use_digits = False
else:
use_digits = True
# Process whether or not to use special characters
if use_arguments:
use_punctuation = not arguments.no_special_chars
else:
while True:
use_punctuation_answer = input("Use special characters [y]/n: ")
if use_punctuation_answer.lower() not in ["", "y", "n"]:
print("Invalid answer")
continue
break
if use_punctuation_answer.lower() == "n":
use_punctuation = False
else:
use_punctuation = True
# Process the password length
if use_arguments_length:
password_length = arguments.length
else:
while True:
password_length_answer = input("Length of the password [10]: ")
if password_length_answer == "":
password_length = 10
break
else:
try:
password_length = int(password_length_answer)
except:
print("Invalid value")
continue
break
letters = string.ascii_letters
digits = string.digits
punctuation = string.punctuation
symbols = letters
if use_digits:
symbols += digits
if use_punctuation:
symbols += punctuation
password = "".join(random.choice(symbols) for i in range(password_length))
print(password)