/
RandomPassword.py
236 lines (168 loc) · 4.9 KB
/
RandomPassword.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import string
import random
newline = "\n"
class RandomPassword:
'''
A class that creates random password.
'''
def __init__(self):
self.lc = string.ascii_lowercase
self.uc = string.ascii_uppercase
self.nr = string.digits
self.special = string.punctuation
def random_lowercase(self):
'''
Returns a random lowercase character.
ARGUMENTS:
----------
None
RETURNS:
----------
random_lc (char)
'''
random_lc = random.choice(self.lc)
return random_lc
def random_uppercase(self):
'''
Returns a random uppercase character.
ARGUMENTS:
----------
None
RETURNS:
----------
random_uc (char)
'''
random_uc = random.choice(self.uc)
return random_uc
def random_nr(self):
'''
Return a random number ( 0-9 ).
ARGUMENTS:
----------
None
RETURNS:
----------
random_nr (char)
'''
random_nr = random.choice(self.nr)
return random_nr
def random_special(self):
'''
Returns a random special character.
ARGUMENTS:
----------
None
RETURNS:
----------
random_special (char)
'''
random_special = random.choice(self.special)
return random_special
def generate_safe_password(self, size = 20):
'''
Generates a safe, random password (as a list !!! ).
The atribute of the function, unless specified otherwise,
will generate 20-char long pass.
ARGUMENTS:
----------
size - (int, default = 20)
RETURNS:
----------
random list - list (of characters)
'''
random_list = list()
while size != 0:
random_step = random.randint(1,4)
if random_step == 1:
x = self.random_lowercase()
random_list.append(x)
elif random_step == 2:
x = self.random_uppercase()
random_list.append(x)
elif random_step == 3:
x = self.random_nr()
random_list.append(x)
else:
x = self.random_special()
random_list.append(x)
size = size - 1
return random_list
def string_from_list(self, my_list):
'''
Turns a list (of random characters) into a string.
ARGUMENTS:
----------
my_list (list)
RETURNS:
----------
my string (string)
'''
my_string = "".join(my_list)
return my_string
def final_result(self):
'''
Sets the final result of the computations, i.e. random password.
Will generate 20-char long pass.
If you need a longer or shorter password,
use final_result_of_given_size()
ARGUMENTS:
----------
None
RETURNS:
----------
b (string)
'''
a = self.generate_safe_password()
b = self.string_from_list(a)
return b
def final_result_of_given_size(self, size):
'''
Generates safe password of given size.
ARGUMENTS:
----------
size (int)
RETURNS:
----------
b (string)
'''
a = self.generate_safe_password(size)
b = self.string_from_list(a)
return b
def generate_n_passwords(self, n, size = 20):
'''
Generates n passwords of size size (by default 20)
ARGUMENTS:
----------
n (int)
size (int, default = 20)
RETURNS:
----------
b (string) <prints nicely!>
'''
counter = 0
b = ""
while n !=0:
counter = counter + 1
a = self.final_result_of_given_size(size)
b = b + " " + str(counter) + " : " + a + newline
n = n - 1
return b
############################
# PROGRAM
############################
if __name__ == "__main__":
mypass = RandomPassword()
a = mypass.final_result()
print("Your new safe password is: ", a)
'''
# DELETE COMMENTS TO GEN 1 pass of size 10
mypass2 = RandomPassword()
a = mypass2.final_result_of_given_size(10)
print("You second password is: ", a)
'''
'''
# DELETE COMMENTS TO GEN 10 passwords of size 5
mypass3 = RandomPassword()
a = mypass3.generate_n_passwords(10, 5)
print(a)
'''