-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordvalidator.py
More file actions
76 lines (68 loc) · 2.52 KB
/
passwordvalidator.py
File metadata and controls
76 lines (68 loc) · 2.52 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
def validate(password):
""" Analyzes an input password to determine if it is "Secure", "Insecure", or "Invalid" based on the assignment description criteria.
Arguments:
password (string): a string of characters
Returns:
result (string): either "Secure", "Insecure", or "Invalid".
"""
if " " in password or "#" in password or "@" in password or len(password) < 8:
return("Invalid")
else:
List = []
for i in password:
if i.islower():
List.append("L")
elif i.isupper():
List.append("U")
elif i.isnumeric():
List.append("N")
elif (i in "!-$%&'()*+,./:;<=>?_[]^`{|}~"):
List.append("C")
if ("L" in List) and ("U" in List) and ("N" in List) and ("C" in List):
return("Secure")
else:
return("Insecure")
pass
def generate(n):
""" Generates a password of length n which is guaranteed to be Secure according to the given criteria.
Arguments:
n (integer): the length of the password to generate, n >= 8.
Returns:
secure_password (string): a Secure password of length n.
"""
import random
import string
characters = list("!-$%&'()*+,./:;<=>?_[]^`{|}~")
password = []
if n < 8:
return
else:
for i in range(n):
password.append(i)
password[0] = random.choice(string.ascii_uppercase)
password[1] = random.choice(string.ascii_lowercase)
password[2] = random.choice(characters)
password[3] = str(random.randint(1,9))
for i in range(4,n):
choose = random.randint(0,3)
if choose == 0:
password[i] = random.choice(string.ascii_uppercase)
elif choose == 1:
password[i] = random.choice(string.ascii_lowercase)
elif choose == 2:
password[i] = random.choice(characters)
elif choose == 3:
password[i] = str(random.randint(1,9))
random.shuffle(password)
password_str = ''.join(password)
return password_str
pass
if __name__ == "__main__":
# Any code indented under this line will only be run
# when the program is called directly from the terminal
# using "python3 validator.py". This can be useful for
# testing your implementations.
print(generate(1))
print(generate(10))
print(generate(20))
pass