-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
182 lines (165 loc) · 5.95 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>leastpass</title>
<link rel="manifest" href="manifest.json">
<style>
body { margin: 10px; }
form * { font-size: x-large; width: 100%; }
form * + * { margin-top: 10px; }
input, select { border: none; text-overflow: ellipsis; }
button { padding: 10px; }
</style>
</head>
<body>
<form id="fields">
<input type="text" id="site" placeholder="site">
<input type="text" id="login" placeholder="login">
<input type="password" id="master_password" type="password" placeholder="master password">
<input type="text" id="alphabit" placeholder="alphabit">
<input type="text" id="required_characters" placeholder="required one of">
<select id="length">
<option value="10">10 characters</option>
<option value="20" selected>20 characters</option>
<option value="30">30 characters</option>
<option value="50">50 characters</option>
<option value="100">100 characters</option>
</select>
<select id="version">
<option value="1" selected>1st password</option>
<option value="2">2nd password</option>
<option value="3">3rd password</option>
<option value="4">4th password</option>
<option value="5">5th password</option>
<option value="6">6th password</option>
<option value="7">7th password</option>
<option value="8">8th password</option>
</select>
<button id="generate_button">Copy password to clipboard</button>
</form>
<script>
const default_alphabit = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
if ('serviceWorker' in navigator) {
window.addEventListener('load', async () => {
const registration = await navigator.serviceWorker.register('service_worker.js');
});
}
async function generate_password({
master_password,
site,
login,
alphabit,
required,
length,
version
}) {
const enc = new TextEncoder('utf-8');
for (let counter = 0; counter < 100; ++counter) {
const encoded_fields = enc.encode([master_password, site, login, version, counter].join(':'));
const salt = new Uint32Array([0x4c4cc505, 0x851e7c8a, 0x99c49016, 0xd2245750, 0x3c9d3d64, 0x13705020, 0xb20600f8, 0xd134d789]);
const key = await crypto.subtle.importKey('raw', encoded_fields, 'PBKDF2', false, ['deriveKey', 'deriveBits']);
const derive_bits = await crypto.subtle.deriveBits(
{
name: 'PBKDF2',
hash: 'SHA-512',
salt,
iterations: 200000
},
key,
512
);
const dv = new DataView(derive_bits);
const bits = Math.ceil(Math.log2(alphabit.length));
const mask = (0x1 << bits) - 1;
let password = '';
let has_required = required.size === 0;
attempt:
for (let i = 0; i < dv.byteLength - 3; i += 4) {
const d = dv.getUint32(i);
for (let j = 0; j < (32 - bits); j += bits) {
const offset = (d >> j) & mask;
if (offset < alphabit.length) {
const ch = alphabit[offset];
password += ch;
if (!has_required) {
has_required = required.has(ch);
}
if (password.length >= length) {
if (has_required) {
return password;
} else {
break attempt;
}
}
}
}
}
}
throw 'could not generate password';
}
function encode_site_url(fields) {
const url = new URL(location.href);
url.hash = `#${btoa(JSON.stringify(fields))}`;
return url.href;
}
function decode_site_url() {
const url = new URL(location.href);
const s = url.hash.substr(1);
return s.length > 0 ? JSON.parse(atob(s)) : {};
}
const site_el = document.getElementById('site'),
login_el = document.getElementById('login'),
master_password_el = document.getElementById('master_password'),
length_el = document.getElementById('length'),
alphabit_el = document.getElementById('alphabit'),
required_characters_el = document.getElementById('required_characters'),
fields_el = document.getElementById('fields'),
generate_button_el = document.getElementById('generate_button'),
version_el = document.getElementById('version');
const url_values = decode_site_url();
const fields = [
['s', site_el, ''],
['l', login_el, ''],
['r', required_characters_el, ''],
['ln', length_el, '20'],
['a', alphabit_el, default_alphabit],
['v', version_el, '1'],
];
for (const [key, el, default_value] of fields) {
el.value = url_values[key] || default_value;
}
fields_el.onchange = e => {
if ([site_el, login_el, alphabit_el, alphabit_el, required_characters_el, length_el, version_el].indexOf(e.target) >= 0)
location.href = encode_site_url(
fields.reduce((a, [key, el, default_value]) => {
if (el.value !== default_value) {
a[key] = el.value;
}
return a;
}, {})
);
};
fields_el.onsubmit = async function () {
try {
const button_content = generate_button_el.innerHTML;
generate_button_el.innerText = 'Generating...';
const password = await generate_password({
master_password: master_password_el.value,
site: site_el.value,
login: login_el.value,
alphabit: alphabit_el.value,
required: new Set(required_characters_el.value),
length: length_el.value,
version: version_el.value
});
navigator.clipboard.writeText(password);
generate_button_el.innerHTML = button_content;
} catch (e) {
console.log(e);
}
};
</script>
</body>
</html>