-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspin.py
453 lines (334 loc) · 17.5 KB
/
aspin.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#Aspin: Filipino-centric Passphrase Generator
#GitHub @unclesocks: https://github.com/UncleSocks
#
#MIT License
#
#Copyright (c) 2024 Tyrone Kevin Ilisan
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
import argparse
import random
import string
class ArgumentParser:
def __init__(self):
self.parser = argparse.ArgumentParser(prog="ASPIN", description="ASPIN is a Filipino-centric passphrase generator.")
self._add_arguments()
def _add_arguments(self):
self.parser.add_argument(
"-l", "--WORDLENGTH",
type=int,
default=5,
dest="word_length",
help="Specify an integer for the number of words in your passphrase. Default value is 5",
)
self.parser.add_argument(
"-n", "--GENERATENUMBER",
type=int,
default=1,
dest="generate_number",
help="Specify the number of passphrases to be generated. Default value is 1."
)
self.parser.add_argument(
"-s", "--SEPARATOR",
type=str,
default="",
dest="separator",
help="Specify a separator character between each word in your passphrase. Default is a no space."
)
self.parser.add_argument(
"-sC", "--SEPARATORCOUNT",
type=int,
default=1,
dest="separator_count",
help="Specify the number of separators between each word. Default value is 1."
)
self.parser.add_argument(
"-N", "--NUMBERS",
action="store_true",
dest="numbers",
help="Append numbers at the end of each word in your passphrase. Default value is False."
)
self.parser.add_argument(
"-S", "--SPECIALCHARS",
action="store_true",
dest="special_characters",
help="Append special characters at the end of each word in your passphrase. Default value is False."
)
self.parser.add_argument(
"-c", '--WORDCASE',
type=str,
default="lowercase",
dest="word_case",
help=(
"Specify a word case for your passphrase. Default value is lowercase. "
"Options are: lowercase, uppercase, capitalize, randomize, alternate."
)
)
self.parser.add_argument(
"-x", "--SUBSTITUTION",
type=str,
default=None,
dest="substitution",
help=(
"Specify a character substitution using the equals (=) symbol between your old and new character (e.g., l=!). "
"Default value is None."
)
)
self.parser.add_argument(
"-w", '--WORDLIST',
type=str,
default="./wordlist/tagalog.txt",
dest="wordlist",
help="Specify the wordlist file location. Default is the tagalog.txt file."
)
self.parser.add_argument(
"-wC", "--WORDLISTCOMBINE",
type=str,
default=None,
dest="wordlist_combine",
help="Specify a second language wordlist to be combined. Default value is None."
)
self.parser.add_argument(
"-i", "--INTERACTIVE",
action="store_true",
dest="interactive",
help="The program will ask for user inputs to generate the passphrase. Default value is False."
)
def parse_arguments(self):
return self.parser.parse_args()
def argument_output(self):
return {
'word_length': self.parser.parse_args().word_length,
'generate_number': self.parser.parse_args().generate_number,
'separator': self.parser.parse_args().separator,
'separator_count': self.parser.parse_args().separator_count,
'numbers': self.parser.parse_args().numbers,
'special_characters': self.parser.parse_args().special_characters,
'substitution': self.parser.parse_args().substitution,
'word_case': self.parser.parse_args().word_case,
'wordlist': self.parser.parse_args().wordlist,
'wordlist_combine': self.parser.parse_args().wordlist_combine
}
class NumbersAndSpecialChars:
def __init__(self, wordlist, word_length):
self.wordlist = wordlist
self.word_length = word_length
def add_special_chars(self):
special_chars = ''.join(random.choice(string.punctuation) for _ in range(self.word_length))
wordlist_with_special_chars = [f"{word}{special_char}" for word, special_char in zip(self.wordlist, special_chars)]
return wordlist_with_special_chars
def add_numbers(self):
numbers = ''.join(random.choice(string.digits) for _ in range(self.word_length))
wordlist_with_numbers = [f"{word}{number}" for word, number in zip(self.wordlist, numbers)]
return wordlist_with_numbers
def special_chars_nums_process(self, numbers_args, special_chars_args):
if special_chars_args == True:
self.wordlist = self.add_special_chars()
if numbers_args == True:
self.wordlist = self.add_numbers()
return self.wordlist
def separator_multiplier(separator, multiplier):
if isinstance(multiplier, int):
multiplied = f"{separator*multiplier}"
return multiplied
else:
ValueError("ERR02: Separator multiplier must be an integer.")
def substitution_parser(substitution):
if substitution:
parsed_substitution = substitution.split("=")
current_char = parsed_substitution[0]
new_char = parsed_substitution[1]
return current_char, new_char
else:
return
def parameter_parser(parameter_dict):
word_length = parameter_dict['word_length']
generate_number = parameter_dict['generate_number']
separator = parameter_dict['separator']
separator_count = parameter_dict['separator_count']
numbers = parameter_dict['numbers']
special_characters = parameter_dict['special_characters']
substitution = parameter_dict['substitution']
word_case = parameter_dict['word_case']
wordlist = parameter_dict['wordlist']
wordlist_combine = parameter_dict['wordlist_combine']
return word_length, generate_number, separator, separator_count, numbers, \
special_characters, substitution, word_case, wordlist, wordlist_combine
class InteractiveGeneration():
def __init__(self):
self.word_length = 5
self.generate_number = 1
self.separator = ""
self.separator_count = 1
self.numbers = False
self.special_characters = False
self.substitution = None
self.word_case = "lowercase"
self.wordlist = "./wordlist/tagalog.txt"
self.wordlist_combine = None
self._collect_user_inputs()
def verify_int_input(self, prompt, default):
user_input = input(prompt)
return int(user_input) if user_input.isdigit() else default
def verify_bool_input(self, prompt):
user_input = input(prompt)
return user_input in ('yes', 'y', 'true', 't', '1')
def _collect_user_inputs(self):
self.word_length = self.verify_int_input(f"\tEnter passphrase word count (default {self.word_length}): ", self.word_length)
self.generate_number = self.verify_int_input(f"\tNumber of passphrase to generate (default {self.generate_number}): ", self.generate_number)
self.separator = input(f"\tSeparator character (default space (default is no space)): ").strip() or self.separator
self.separator_count = self.verify_int_input(f"\tEnter separator count (default {self.separator_count}): ", self.separator_count)
self.numbers = self.verify_bool_input(f"\tInclude numbers (default {self.numbers}): ")
self.special_characters = self.verify_bool_input(f"\tInclude special characters (default {self.special_characters}): ")
self.substitution = input(f"\tSpecify character substitution (default {self.substitution}): ") or self.substitution
self.word_case = input (f"\tWord case (default {self.word_case}): ") or self.word_case
self.wordlist = input(f"\tWordlist file path (defult {self.wordlist}): ") or self.wordlist
self.wordlist_combine = input(f"\tCombine another wordlist (default {self.wordlist_combine}): ") or self.wordlist_combine
def output(self):
return {
'word_length': self.word_length,
'generate_number': self.generate_number,
'separator': self.separator,
'separator_count': self.separator_count,
'numbers': self.numbers,
'special_characters': self.special_characters,
'substitution': self.substitution,
'word_case': self.word_case,
'wordlist': self.wordlist,
'wordlist_combine': self.wordlist_combine
}
class WordlistProcess:
def __init__(self, filepaths):
self.filepaths = filepaths
def count_lines(self):
filepath_dict = {}
for filepath in self.filepaths:
with open(filepath, 'rb') as file:
lines_sum = sum(1 for _ in file)
#The underscore (_) is a common variable placeholder for variable values not being used.
filepath_dict.update({filepath:lines_sum})
return filepath_dict
def get_word(self, line_number, chosen_filepath):
with open(chosen_filepath, 'r') as file:
for current_line, line in enumerate(file, start=1):
if current_line == line_number:
return line.strip()
def process_wordlist(self, word_length):
filepath_lines_sum = self.count_lines()
wordlist = []
while len(wordlist) < word_length:
randomized_filepath_index = random.randint(0,len(self.filepaths) - 1)
chosen_filepath = self.filepaths[randomized_filepath_index]
line_number = random.randint(1, filepath_lines_sum[chosen_filepath])
current_word = self.get_word(line_number, chosen_filepath)
if current_word and current_word not in wordlist:
wordlist.append(current_word)
return wordlist
class WordCase:
def __init__(self, wordlist, word_case):
self.wordlist = wordlist
self.word_case = word_case
def lowercase(self):
lowercase_wordlist = [word.lower() for word in self.wordlist]
return lowercase_wordlist
def uppercase(self):
uppercase_wordlist = [word.upper() for word in self.wordlist]
return uppercase_wordlist
def capitalize(self):
capitalize_wordlist = [word.capitalize() for word in self.wordlist]
return capitalize_wordlist
def randomize(self):
randomize_wordlist = []
for word in self.wordlist:
randomize_word = ''.join(random.choice([char.upper(), char]) for char in word)
randomize_wordlist.append(randomize_word)
return randomize_wordlist
def alternate(self):
alternate_wordlist = []
for word in self.wordlist:
alternate_word = ''.join(char.upper() if index % 2 == 0 else char.lower() for index, char in enumerate(word))
alternate_wordlist.append(alternate_word)
return alternate_wordlist
def process(self):
word_case_method = {
'lowercase':self.lowercase,
'uppercase':self.uppercase,
'capitalize':self.capitalize,
'randomize':self.randomize,
'alternate':self.alternate
}
if self.word_case in word_case_method:
return word_case_method[self.word_case]()
else:
ValueError(f"ERR01: Unsupported word case method: {self.word_case}")
def banner():
aspin_banner = """
████ ██
█ ███ ███
████ ██ ███
█ ███████████ ██ ███ ██ █████████
████ █████ ███ ███ ██ ███ ███ ████████ █████ ████████
████ █████████ ███ ██████ ████ ████ ██ ███ ███ ███
███ ████ ███ ███ ███ ███ ███ █████ ███ ███ ███ ███
███ ████ ██ ██ ███ ████ ███ ████ ██ ██ ███ ███ ███
███ ████ ██ █████ ███ ███████ █ ███ ██ ███
███ █████ ████ ███ ██ ████ ███
██████ ██ ███████ █
██████ ███████
██ ██
ASPIN: Filipino-centric Passphrase Generator
\t[+] Generates randomized passphrases
\t[+] Use the `-h` option for help
\t@unclesocks
\thttps://github.com/UncleSocks/aspin-filipino-centric-passphrase-generator
\t-------------------------------------------------------------------------------------------------
"""
return print(aspin_banner)
def aspin():
banner()
argument_parser = ArgumentParser()
argument = argument_parser.parse_arguments()
init_generate_count = 0
if argument.interactive:
user_input_parameters = InteractiveGeneration()
parameter_output = user_input_parameters.output()
else:
parameter_output = argument_parser.argument_output()
word_length, generate_number, separator, separator_count, numbers, special_characters, \
substitution, word_case, wordlist, wordlist_combine = parameter_parser(parameter_output)
wordlist_list = []
wordlist_list.append(wordlist)
if wordlist_combine and wordlist_combine is not None:
wordlist_list.append(wordlist_combine)
print("\n\tPassphrase Generated:\n\t")
while init_generate_count < generate_number:
process_wordlist = WordlistProcess(wordlist_list)
wordlist = process_wordlist.process_wordlist(word_length)
multiplied_separator = separator_multiplier(separator, separator_count)
nums_and_special_chars = NumbersAndSpecialChars(wordlist, word_length)
wordlist = nums_and_special_chars.special_chars_nums_process(numbers, special_characters)
wordlist = WordCase(wordlist, word_case).process()
passphrase_raw = f'{multiplied_separator}'.join(wordlist)
if substitution and substitution is not None:
current_char, new_char = substitution_parser(substitution)
passphrase_raw = passphrase_raw.replace(current_char, new_char)
print(f"\t{passphrase_raw}")
init_generate_count += 1
print("\n")
if __name__ == "__main__":
aspin()