-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserGen.py
More file actions
237 lines (181 loc) · 6.99 KB
/
UserGen.py
File metadata and controls
237 lines (181 loc) · 6.99 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
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
__author__ = 'Denis C.'
__date__ = '2020.05.20'
__version__ = '0.1'
"""
Generator of password, user name and email for a specific user
The generator starts by n encrypting the user name
with 5 diferent algorithms, taking the first NUM_HASH
characters from each one and joining them.
"""
import hashlib
import random
import json
import argparse
import sys
from os import path
""" Class that generates most things needed for a user: email, username and password """
class UserGenerator:
def __init__(self, names, prefix, sufix, joiner):
self.names = names
self.prefix = prefix
self.sufix = sufix
self.joiner = joiner
def chanceJoiner(self, chance, str1, str2):
if random.random() <= chance:
return (str1 + random.choice(self.joiner) + str2)
else:
return (str1 + str2)
""" Function that generates a password that will alwas¡ys have the same results depending on the user name"""
def genPassword(self, user):
# All the encription functions and how many letter will be taken from each hash result
# It is recommecomended to modify this when using this script for personal reasons
ENCRYPT_FUNCS = [(hashlib.sha1, 3), (hashlib.md5, 4), (hashlib.sha224, 3),
(hashlib.sha512, 5), (hashlib.sha384, 6)]
MAX_END_lEN = 7
passwd = ""
user = user.encode('utf-8')
for i in range(len(ENCRYPT_FUNCS)):
passwd += ENCRYPT_FUNCS[i][0](user).hexdigest()[:ENCRYPT_FUNCS[i][1]]
#
ending = len(user) % MAX_END_lEN
passwd += passwd[:ending] + '?'
# Capitalize the first found letter
for i, char in enumerate(passwd):
if(char.isalpha()):
passwd = passwd [:i] + passwd[i:].capitalize()
break
return passwd
def genUserName(self):
# hance of the username having a sufix / prefix
SUFIX_CHANCE = 0.80
PREFIX_CHANCE = 0.80
# Chance of using a joiner when concatenating strings
JOINER_CHANCE = 0.40
# If the username lengh is less that this threshold, a sufix is guaranteed
NAME_PREFIX_THRESHOLD = 10
# Chance of the first name to just be an initial
INITIAL_CHANCE = 0.30
# Chance of the second name being an initial of the first name is not
SECOND_INITIAL_FULL = 0.50
# Chance of the second name being an intial if the first one is as well
SECOND_INITIAL_PARCIAL = 0.30
# Chance of a third initial
THIRD_INITIAL = 0.40
# Chance of a third name
THIRD_NAME = 0.20
user_prefix = ''
user_sufix = ''
user_name = random.choice(self.names)
second_name = random.choice(self.names)
third_name = random.choice(self.names)
# The username has a n chance of having a prefix
if random.random() <= PREFIX_CHANCE:
user_prefix = random.choice(self.prefix)
# N chance of the first name to just be the initial
if random.random() <= INITIAL_CHANCE:
user_name = user_name[0]
# Chance of the second name also being an initial, the third name gets appended
if random.random() <= SECOND_INITIAL_PARCIAL:
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, second_name)
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, third_name)
else:
user_name += second_name
# Chance of it having a third initial if the second name is appended fully
if random.random() <= THIRD_INITIAL:
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, third_name[0])
else:
# Chance of the second name being an intial if the first one isnt
if random.random() <= SECOND_INITIAL_FULL:
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, second_name[0])
else:
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, second_name)
# Chance of a third name
if random.random() <= THIRD_NAME:
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, third_name)
else:
user_name = self.chanceJoiner(JOINER_CHANCE, user_name, third_name[0])
# If the lenght is less than the threashold, a sufix is guaranteed, if not its chance
if len(user_name) < NAME_PREFIX_THRESHOLD:
user_sufix = random.choice(self.sufix)
else:
# Chance of a sufix
if random.random() < SUFIX_CHANCE:
user_sufix = random.choice(self.sufix)
user_name = user_prefix + user_name + user_sufix
return user_name
def genEmail(self, user_name):
# All possible email endings
EMAIL_END = ["@gmail.com", "@hotmail.com", "@yahoo.com"]
return user_name + random.choice(EMAIL_END)
def genUsers(self, num):
NAME = 'name'
PASS = 'password'
users = {}
for i in range(num):
user_name = self.genUserName()
users[self.genEmail(user_name)] = {
NAME: user_name,
PASS: self.genPassword(user_name)
}
return users
def exportUsersJson(self, num, file):
users = self.genUsers(num)
with open(file, 'w') as json_file:
json.dump(users, json_file)
return users
# class User:
# JSON_NAME = 'name'
# JSON_PASS = 'password'
# def __init__(self, name, email, password):
# self.name = name
# self.email = email
# self.password = password
# def dataFormat(self):
# data = {}
# data[self.email].append({
# self.JSON_PASS: self.password,
# self.JSON_NAME: self.name,
# })
# return data
if __name__ == "__main__":
my_parser = argparse.ArgumentParser(description='Generates user names, emails and passwords in JSON format')
# Add the arguments
my_parser.add_argument('Count', metavar='Count', type=int,
help='the number of generated users')
my_parser.add_argument('-p', '--passwd', action='store', metavar='USERNAME',
help='check the password of the inputed user name')
my_parser.add_argument('-o', '--output', action='store', metavar='PATH',
help='file to output the users in JSON format', default='export.json')
my_parser.add_argument('-n', '--names', action='store', metavar='PATH',
help='JSON file with various names and surnames', default='names.json')
my_parser.add_argument('-j', '--joiner', action='store', metavar='PATH',
help='JSON file with joiners of names and surnames', default='joiner.json')
my_parser.add_argument('-su', '--suffix', action='store', metavar='PATH',
help='file with various suffixes added to the usernames', default='suffixes.json')
my_parser.add_argument('-pre', '--prefix', action='store', metavar='PATH',
help='file with various prefixes added to the usernames', default='prefixes.json')
args = my_parser.parse_args()
count = args.Count
check_password = args.passwd
output = args.output
try:
file_names = open(args.names).read()
file_joiner = open(args.joiner).read()
file_suffix = open(args.suffix).read()
file_prefix = open(args.prefix).read()
names = json.loads(file_names)
joiner = json.loads(file_joiner)
suffixes = json.loads(file_suffix)
prefixes = json.loads(file_prefix)
except IOError as error:
print("Couldn´t open file: " + error.filename, file=sys.stderr)
sys.exit(1)
except (json.JSONDecodeError, TypeError) as JSONError:
print(JSONError, file=sys.stderr)
sys.exit(2)
finally:
generator = UserGenerator(names, prefixes, suffixes, joiner)
if check_password is not None:
print (generator.genPassword(check_password))
if count != 0:
print(generator.exportUsersJson(count, output))