forked from moiaune/newpasswd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newpasswd.py
executable file
·197 lines (156 loc) · 5.63 KB
/
newpasswd.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
#!/usr/bin/env python3
from random import randint, shuffle
import getopt
import sys
import re
import os
import shutil
import urllib.request
STR_LENGTH = 16
UPPERCASE = False
LOWERCASE = False
DIGITS = False
SYMBOLS = False
COUNT = 1
NUMOFWORDS = 2
PREFIX = False
SUFFIX = False
WORD = False
DELIMITER = "-"
PROJECT_FOLDER = os.path.expanduser('~/.newpasswd')
WORDFILE_URL = "https://raw.githubusercontent.com/madsaune/newpasswd/master/data/wordlist.txt"
def usage():
print("Usage: newpasswd [-FLAGS] [-c COUNT] [-n NUMOFWORDS] [-b DELIMITER]")
print("")
print("Generates either a string of random characters or words with optional 4 digits as prefix/suffix.")
print("")
print("arguments:")
print("\t-h, --help\t\t Show this help message")
print("\t-u, --uppercase\t\t Include UPPERCASE characters (A-Z)")
print("\t-l, --lowercase\t\t Include lowercase characters (a-z)")
print("\t-d, --digits\t\t Include digits (0-9)")
print("\t-s, --symbols\t\t Include symbols (!@#|_-*)")
print("\t-p, --prefix\t\t Add prefix to password (4 digits)")
print("\t-x, --suffix\t\t Add suffix to password (4 digits)")
print("\t-w, --word\t\t Generate sentence (without, a random string will be generated)")
print("\t-c, --count <num>\t How many passwords to generate")
print("\t-n, --numOfWords <num>\t How many words to include. Only works with -w")
print("\t-z, --size <num>\t How many characters to include. Only works without -w")
print("\t-b, --between <char>\t Character(s) to seperate words. Only works with -w")
print("\t --delimiter <char>")
print("")
print("examples:")
print("\tGenerate a string of 2 random words with delimiter and suffix.")
print("\t\t$ newpasswd -wx")
print("")
print("\tGenerate a string of 12 random characters, containing uppercase, lowercase, digits and symbols.")
print("\t\tnewpasswd -uldsz12")
def first_run():
if not os.path.exists(PROJECT_FOLDER):
os.mkdir(PROJECT_FOLDER)
if not os.path.isfile(os.path.join(PROJECT_FOLDER, 'wordlist.txt')):
with urllib.request.urlopen(WORDFILE_URL) as response:
with open(os.path.join(PROJECT_FOLDER, 'wordlist.txt'), 'wb') as destination:
shutil.copyfileobj(response, destination)
destination.close()
first_run()
try:
opts, args = getopt.getopt(sys.argv[1:], "huldspxwc:n:z:b:", ['help', 'uppercase', 'lowercase', 'digits', 'symbols', 'prefix', 'suffix', 'word', 'count=', 'numOfWords=', 'size=', 'between=', 'delimiter='])
if len(opts) < 1:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(0)
elif opt in ('-u', '--uppercase'):
UPPERCASE = True
elif opt in ('-l', '--lowercase'):
LOWERCASE = True
elif opt in ('-d', '--digits'):
DIGITS = True
elif opt in ('-s', '--symbols'):
SYMBOLS = True
elif opt in ('-p', '--prefix'):
PREFIX = True
elif opt in ('-x', '--suffix'):
SUFFIX = True
elif opt in ('-w', '--word'):
WORD = True
elif opt in ('-c', '--count'):
COUNT = int(arg)
elif opt in ('-n', '--numOfWords'):
NUMOFWORDS = int(arg)
elif opt in ('-z', '--size'):
STR_LENGTH = int(arg)
elif opt in ('-b', '--between', '--delimiter'):
DELIMITER = arg
else:
usage()
sys.exit(2)
except getopt.GetoptError:
usage()
sys.exit(2)
def generateKeyspace():
keyspace = ""
keyspace += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if UPPERCASE else ""
keyspace += "abcdefghijklmnopqrstuvwxyz" if LOWERCASE else ""
keyspace += "0123456789" if DIGITS else ""
keyspace += "!@#|_-*" if SYMBOLS else ""
keyspace = list(keyspace)
shuffle(keyspace)
return ''.join(keyspace)
def isValid(str):
isValid = True
if UPPERCASE and re.search(r"[A-Z]", str) is None:
isValid = False
if LOWERCASE and re.search(r"[a-z]", str) is None:
isValid = False
if DIGITS and re.search(r'[\d]', str) is None:
isValid = False
if SYMBOLS and re.search(r'[!@#|_\-*]', str) is None:
isValid = False
return isValid
def generateString():
chars = generateKeyspace()
global STR_LENGTH
if PREFIX:
STR_LENGTH -= 4
if SUFFIX:
STR_LENGTH -= 4
for x in range(COUNT):
recalc = True
password = ""
while not isValid(password):
password = ""
prefixData = ""
suffixData = ""
if PREFIX:
prefixData = str(randint(1111, 9999))
if SUFFIX:
suffixData = str(randint(1111, 9999))
for y in range(STR_LENGTH):
password += chars[randint(0, len(chars) - 1)]
print(password)
def generateSentence():
f = open(os.path.join(PROJECT_FOLDER, 'wordlist.txt'), 'r')
wordlist = f.readlines()
f.close()
for x in range(0, COUNT):
list_of_words = []
password = ""
if PREFIX:
list_of_words.append(str(randint(1111, 9999)))
for y in range(0, NUMOFWORDS):
list_of_words.append(wordlist[randint(0, len(wordlist) - 1)].rstrip().capitalize())
if SUFFIX:
list_of_words.append(str(randint(1111, 9999)))
password = DELIMITER.join(list_of_words)
print(password)
def main():
if WORD:
generateSentence()
else:
generateString()
if __name__ == '__main__':
main()