-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGen.py
More file actions
132 lines (116 loc) · 4.73 KB
/
Copy pathKeyGen.py
File metadata and controls
132 lines (116 loc) · 4.73 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
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
import random
# from LearningPython import FileHandler
class KeyGen:
key = []
lower = []
numbers = []
symbols = []
upper = []
role = None
values = ('y', 'n')
'''
constructor that generates 4 lists,
for lowercase, uppercase, numbers and symbols,
from ASCII characters
'''
def __init__(self, role):
self.role = role
i = 0
for number in range(33, 127):
i += 1
# print(number , ' - ', chr(number))
if 48 <= number <= 57:
self.numbers.append(chr(number))
elif 65 <= number <= 90:
# print(number)
self.upper.append(chr(number))
elif 97 <= number <= 122:
self.lower.append(chr(number))
else:
self.symbols.append(chr(number))
self.create_menu()
'''
creates a 2 options menu, which allows you to choose
between closing the program or generating a new password
'''
def create_menu(self):
print('Welcome to KeyGen program')
option = ''
while option != 'q':
print('*' * 5, 'press c for create a new password')
print('*' * 5, 'press q for exit')
option = input('select your choice:').lower()
if option == 'c':
self.define_key_parameters()
'''
Interact with the user to define the password parameters.
The outputs of the method itself explain its operation
'''
def define_key_parameters(self):
print('Introduce the parameters for the new password: ')
ch_number = input('*' * 5 + 'select key\'s characters number: (between 8 and 12)')
while not ch_number.isnumeric() or int(ch_number) not in range(8, 13):
print('please enter a valid option:')
ch_number = input('*' * 5 + 'select key\'s characters number: (between 8 and 12)')
option = ''
characters = ('lowercase', 'uppercase', 'numbers', 'symbols')
ch_included = []
for ch_type in characters:
while option not in self.values:
print('enter y (yes) or n (no)')
option = input('*' * 5 + f'Would you like include {ch_type}?').lower()
if option == 'y':
if ch_type == 'lowercase':
ch_included.extend(self.lower)
elif ch_type == 'uppercase':
ch_included.extend(self.upper)
elif ch_type == 'numbers':
ch_included.extend(self.numbers)
else:
ch_included.extend(self.symbols)
print(f'ok, {ch_type} included')
option = None
if not ch_included:
print('no characters have been selected, at least the lower case will be included in the password')
ch_included.extend(self.lower)
if self.role == 'admin':
num_keys = input('How many passwords do you want to create? (100 maximum)')
while not num_keys.isnumeric() or int(num_keys) not in range(1, 101):
print('please enter a valid option:')
num_keys = input('How many passwords do you want to create? (100 maximum)')
self.admin_key_generator(ch_number, ch_included, int(num_keys))
else:
self.key_generator(ch_number, ch_included)
'''
create a password with user-defined parameters, and send it to standard output
'''
def key_generator(self, ch_number, ch_included):
for i in range(0, int(ch_number)):
self.key.append(ch_included[random.randint(0, (len(ch_included) - 1))])
print('A new password has been created: ' + ''.join(self.key))
self.key.clear()
def admin_key_generator(self, ch_number, ch_included, num_keys):
file = input('type a name for the file')
file += '.txt'
option = ''
while option not in self.values:
print('If the file already exists, do you want to overwrite it?')
option = input('Enter y (yes) or n (no)').lower()
try:
# obj = FileHandler.FileHandler()
if option == 'y':
f = open(file, 'w+')
else:
f = open(file, 'a+')
for pass_num in range(0, num_keys):
for i in range(0, int(ch_number)):
self.key.append(ch_included[random.randint(0, (len(ch_included) - 1))])
f.write(''.join(self.key)+'\n')
self.key.clear()
f.close()
except IOError:
print('There was a problem opening the file')
else:
print(f'passwords have been written into {file}')
# test = KeyGen()
# print(test.symbols)