-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordGenerator
123 lines (115 loc) · 5.57 KB
/
PasswordGenerator
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
def getLength():
#This function prompts the user to enter the desired password length,
#checks if the input is a positive integer, and returns the length value.
while True:
try:
length = int(input("\nPlease enter the length of your password: "))
if length <= 0:
print("The length must be a positive integer number. Please try again.")
continue
break
except ValueError:
print("The length must be a positive integer number.")
return length
def getComplexity():
#This function prompts the user to choose a complexity level for the password,
#displays a description of each level, checks if the input is a number between 1 and 5,
#and returns the complexity value.
print("\nPlease choose the complexity level of your password, according to the following description:")
print("Level 1: very low: contains only digits")
print("Level 2: low: contains digits and some letters")
print("Level 3: moderate: contains digits and letters approximately equally")
print("Level 4: strong: contains digits, letters and some special characters")
print("Level 5: very strong: contains digits, letters and special characters approximately equally")
while True:
try:
complexity = int(input("\nPlease enter the complexity level (1-5): "))
if complexity not in range(1,6):
print("The complexity must be an integer between 1 to 5.")
continue
break
except ValueError:
print("The complexity must be an integer between 1 to 5.")
return complexity
def passwordSegments(length, complexity):
#This function takes in the desired password length and complexity level,
#determines the number of characters from each segment that the password should contain,
#and returns a list of these segment counts.
segments = [0,0,0] #segments[0] = digits, segments[1] = letters, segments[2] = special chars
if complexity == 1:
segments[0], segments[1], segments[2] = length, 0, 0
elif complexity == 2:
segments[0], segments[1], segments[2] = int(length*0.8), length-int(length*0.8), 0
elif complexity == 3:
segments[0], segments[1], segments[2] = int(length*0.5), length-int(length*0.5), 0
elif complexity == 4:
segments[0], segments[1], segments[2] = int(length*0.4), int(length*0.4), length-int(length*0.4)-int(length*0.4)
else:
segments[0], segments[1], segments[2] = int(length*(1/3)), int(length*(1/3)), length-int(length*(1/3))-int(length*(1/3))
return segments
def getSpecialChars(complexity):
#This function prompts the user to choose whether to use default special characters
#or to enter their own special characters. If the user chooses to enter their own characters,
#the function checks that the input contains only special characters and returns the special characters string.
specialChars = "!@#$%^&*()-_+=/.,<>:;'{}\|~`" #built in chars.
if complexity > 3:
#if complexity <= 3 it means that the password will contain only digits and letters!
while True:
choice = input("Do you want to use the default special characters? (y/n): ")
while True:
if choice.lower() == "n":
specialChars = input("Enter your special characters: ")
temp = ''
for i in range(len(specialChars)): #removes digits and letters. keeps only special chars.
if specialChars[i].isalnum() == False:
temp += specialChars[i]
specialChars = ''.join(temp)
if len(specialChars) < 1:
print("You must enter at least one special character.")
else:
return specialChars
elif choice.lower() != "y":
print("Enter n or y only.")
break
else:
return specialChars
def generatePassword(length, complexity):
#The function takes in length and complexity given by the user and generates a password.
#Firstly creates the segments of the characters that can be in the password.
#After that randomlly picks from each segment and builds the password.
newPassword = []
segmentsCount = passwordSegments(length, complexity) #a counter list for the amount of chars from each segment.
segments = ["0123456789", "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"]
segments.append(getSpecialChars(complexity))
for i in range(length): #builds the password
pickSegment = random.randint(0,2)
while segmentsCount[pickSegment] == 0:
pickSegment = random.randint(0,2)
newPassword.append(random.choice(segments[pickSegment]))
segmentsCount[pickSegment] -= 1
newPassword = ''.join(newPassword)
return newPassword
def startAgain():
#a function for letting the user to start again the program.
choice = input("\nStart again? (y/n): ")
choice = choice.lower()
if choice == 'y':
main()
elif choice == 'n':
return
else:
print("Type y or n only please.")
startAgain()
def main():
print("Hello!")
length = getLength()
complexity = getComplexity()
while length < complexity:
print("Password length cannot be less than complexity level. Please try again.")
length = getLength()
complexity = getComplexity()
password = generatePassword(length, complexity)
print(".\n.\n.\n.\n.\nYour password is: ", password)
startAgain()
main()