-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass_gen.py
More file actions
executable file
·158 lines (127 loc) · 3.59 KB
/
Copy pathpass_gen.py
File metadata and controls
executable file
·158 lines (127 loc) · 3.59 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
#!/usr/bin/env python3
#
# pass_gen.py
#
# Password generator. Tries to balance memorizability and complexity.
#
from subprocess import run
from random import randrange,getrandbits,choice,shuffle
from os import access, R_OK
from os.path import isfile, expanduser
from pathlib import Path
import urllib.request
#
##
### define variables ###
##
#
wordlist_url = 'https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-no-swears.txt'
directory = expanduser('~') + '/.evscripts'
wordlist = directory + '/google-10000-english-no-swears.txt'
min_pass_length = 14
max_pass_length = 20
min_word_length = 3
max_word_length = 12
min_words = 2
max_words = 4
min_digits = 1
max_digits = 2
min_symbols = 1
max_symbols = 2
grep_regex = '^[^#]{%i,%i}$' % (min_word_length, max_word_length)
symbols = list('~!@#$%^&*=+-_?/\\')
max_num = 100
password = ''
#
##
### functions ###
##
#
# rand_capitals function.
# takes a string as an argument. returns the
# string with a random, pre-defined capitalization
# scheme applied.
def rand_capitals(word):
choice = randrange(1, 4)
if choice == 1: # leave lowercase
return word
elif choice == 2: # capitalize first letter
return word.capitalize()
elif choice == 3: # all uppercase
return word.upper()
# check_criteria function.
# takes a password as an argument. determines whether the
# password matches security criteria. returns True or False.
def check_criteria(pw):
# Must be above min length
if len(password) < min_pass_length:
return False
# Must be below max length
if len(password) > max_pass_length:
return False
# Must have uppercase
if password == password.lower():
return False
# Must have lowercase
if password == password.upper():
return False
# Must have symbol
if not any(char in symbols for char in password):
return False
# Must have digit
if not any(char in ('0','1','2','3','4','5','6','7','8','9') for char in password):
return False
# Passed all checks
return True
#
##
### main ###
##
#
# create directory for wordlist if doesn't exist
Path(directory).mkdir(exist_ok=True)
# download wordlist if needed
if not ( isfile(wordlist) and access(wordlist, R_OK) ):
urllib.request.urlretrieve(wordlist_url, wordlist)
# create list of words that match the length criteria
grep_proc = run(
['grep', '-P', grep_regex, wordlist],
capture_output=True,
encoding='ASCII',
check=True
)
words = grep_proc.stdout.strip().split('\n')
# continue looping until password meets requirements
while(check_criteria(password) == False):
# reset variables
elements = []
contains_non_words = True
#
##
### build list of elements ###
##
#
# words
for i in range(randrange(min_words, max_words + 1)):
elements.append(rand_capitals(choice(words)))
# mutate first word
cut_letter = randrange(1, len(elements[0]) + 1)
elements[0] = elements[0][0:cut_letter-1] + elements[0][cut_letter:]
# confirm new word not in wordlist
for w in words:
if w.lower() == elements[0].lower():
contains_non_words = False
break
if not contains_non_words:
continue
# symbols
for i in range(randrange(min_symbols, max_symbols + 1)):
elements.append(choice(symbols))
# digits
for i in range(randrange(min_digits, max_digits + 1)):
elements.append(str(randrange(0, max_num)))
# shuffle and assemble
shuffle(elements)
password = ''.join(elements)
# print final password
print(password)