-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpassgen
executable file
·177 lines (154 loc) · 5.21 KB
/
passgen
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
#!/usr/bin/env python3
# Copyright (c) Vivek Venugopalan, 2020.
import hashlib
# Standard Library
import random
import string
import urllib.error
import urllib.parse
import urllib.request
from argparse import ArgumentParser
def copy_clipboard(data):
try:
import pyperclip
pyperclip.copy(data)
except ModuleNotFoundError:
pass
def check_item(dataset, pwd, flag):
return len(set(dataset).intersection(set(pwd))) > 0 if flag else True
def check_available(pwd, uppercase, digits, punctuation):
if not check_item(string.ascii_uppercase, pwd, uppercase):
return False
if not check_item(string.digits, pwd, digits):
return False
if not check_item(string.punctuation, pwd, punctuation):
return False
return True
def print_qrcode(pwd):
try:
import pyqrcode
return pyqrcode.create(pwd).terminal(quiet_zone=1)
except ModuleNotFoundError as e:
print(e)
return None
def gen_password(
length=10,
left_hand=False,
right_hand=False,
uppercase=False,
mixedcase=True,
digits=True,
punctuation=True,
):
population = None
if left_hand:
population = "12345qwertyasdfgzxcv"
if uppercase:
population += "QWERTYASDFGZXCV"
elif right_hand:
population = "[];',/.7890-uiopjklnmHhb"
if uppercase:
population += "UIOPJKLNMHB"
else:
population = string.ascii_uppercase if uppercase else string.ascii_lowercase
population += string.ascii_uppercase if mixedcase else ""
population += string.digits if digits else ""
population += string.punctuation if punctuation else ""
rnd = random.SystemRandom()
pwd = "".join(rnd.sample(population, length))
while not check_available(pwd, uppercase, digits, punctuation):
pwd = "".join(rnd.sample(population, length))
return pwd
def check_compromised(pwd):
hashedpwd = hashlib.sha1(pwd.encode("utf-8")).hexdigest().upper()
hashedprefix = hashedpwd[0:5]
hashedremaining = hashedpwd[5:]
req = urllib.request.Request(
"https://api.pwnedpasswords.com/range/" + hashedprefix,
None,
{
"User-agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
},
)
stream = urllib.request.urlopen(req)
for item in stream:
# Convert byte stream to string https://stackoverflow.com/a/606199
str_item = item.decode("utf-8")
if hashedremaining == str_item.split(":")[0]:
return True
return False
if __name__ == "__main__":
parser = ArgumentParser()
uc_group = parser.add_mutually_exclusive_group()
uc_group.add_argument(
"-u",
"--uppercase",
dest="uppercase",
action="store_true",
help="Use ONLY uppercase letters",
)
uc_group.add_argument(
"-o",
"--mixedcase",
dest="mixedcase",
action="store_true",
help="Use mixed case letters (default)",
)
uc_group.set_defaults(mixedcase=True)
parser.add_argument("-n", "--number", dest="number", action="store_true")
parser.add_argument("-N", "--no-number", dest="number", action="store_false")
parser.set_defaults(number=True)
punc_group = parser.add_mutually_exclusive_group()
punc_group.add_argument(
"-p", "--punctuation", dest="punctuation", action="store_true"
)
punc_group.add_argument(
"-P", "--no-punctuation", dest="punctuation", action="store_false"
)
punc_group.set_defaults(punctuation=False)
parser.add_argument(
"-t", "--length", type=int, action="store", dest="length", default=10
)
char_group = parser.add_mutually_exclusive_group()
char_group.add_argument(
"-L", "--no-lefthand", dest="left_hand", action="store_false"
)
char_group.add_argument("-l", "--lefthand", dest="left_hand", action="store_true")
char_group.set_defaults(left_hand=False)
char_group.add_argument(
"-R", "--no-righthand", dest="right_hand", action="store_false"
)
char_group.add_argument("-r", "--righthand", dest="right_hand", action="store_true")
parser.set_defaults(right_hand=False)
print_group = parser.add_mutually_exclusive_group()
print_group.add_argument("-q", "--qrcode", dest="qrcode", action="store_true")
parser.set_defaults(qrcode=False)
args = parser.parse_args()
pwd = gen_password(
length=args.length,
left_hand=args.left_hand,
right_hand=args.right_hand,
uppercase=args.uppercase,
mixedcase=args.mixedcase,
digits=args.number,
punctuation=args.punctuation,
)
count = 1
while check_compromised(pwd):
print("compromised password generated. Generating {0} password".format(count))
count += 1
pwd = gen_password(
length=args.length,
left_hand=args.left_hand,
right_hand=args.right_hand,
uppercase=args.uppercase,
mixedcase=args.mixedcase,
digits=args.number,
punctuation=args.punctuation,
)
copy_clipboard(pwd)
if args.qrcode:
code = print_qrcode(pwd)
print(code if code else pwd)
else:
print(pwd)