forked from dheera/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassgen
More file actions
executable file
·95 lines (70 loc) · 2.23 KB
/
passgen
File metadata and controls
executable file
·95 lines (70 loc) · 2.23 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
#!/usr/bin/env python3
# my (almost) state-less password manager
import hashlib, os, re, sys, base64, time, json
script_path = os.path.realpath(__file__)
try:
# try hard
from getpass import getpass
except ImportError:
# stop complaining and try harder
os.system("sudo pip3 install getpass")
from getpass import getpass
# domain name in question, e.g. google.com
domain = sys.argv[1]
# load parameters for different sites' nitpicks about password requirements
params = {}
with open(os.path.dirname(os.path.realpath(__file__)) + '/passgen-params.json', 'r') as f:
try:
params = json.loads(f.read()).get(domain, {})
except:
print("Error loading params")
# have user input master password
master_password = getpass()
# nth password used on site
if 'n' not in params:
params['n'] = 0
# tack this onto the end of a password
# useful if a site requires a use of a particular restricted set of symbols
if 'tail' not in params:
params['tail'] = ""
# ensure an uppercase character
if 'ensure-upper' not in params:
params['ensure-upper'] = True
# ensure an lowercase character
if 'ensure-lower' not in params:
params['ensure-lower'] = True
# ensure a number
if 'ensure-number' not in params:
params['ensure-number'] = True
# ensure a symbol
if 'ensure-symbol' not in params:
params['ensure-symbol'] = True
# length of password
if 'length' not in params:
params['length'] = 16
# generate password
thehash = hashlib.pbkdf2_hmac(
'sha256',
(master_password + "/" + domain).encode('utf-8'),
b'',
200000 + params['n'],
)
password_text = bytearray(base64.b64encode(thehash)
.replace(b'+', b'')
.replace(b'/', b'')
.replace(b'=', b'')
)
if params['ensure-upper']:
password_text[0] = 0x41 + (password_text[0] % 26)
if params['ensure-lower']:
password_text[1] = 0x61 + (password_text[0] % 26)
if params['ensure-number']:
password_text[2] = 0x30 + (password_text[0] % 10)
if params['ensure-symbol']:
password_text[3] = 0x21 + (password_text[0] % 10)
password_text = password_text.decode()
if "exclude-chars" in params:
for char in params["exclude-chars"]:
password_text = password_text.replace(char, "")
password = password_text[0:params['length']] + params['tail']
print(password)