-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencdec.html
467 lines (422 loc) · 14.2 KB
/
encdec.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,height=device-height, user-scalable=no" />
<meta name="description" content="Encryption / decvryption library">
<title>EncDec Lib Demo</title>
<link rel="stylesheet" href="css/properties-editor.css" />
<link rel="stylesheet" href="css/fontello.css" />
<link rel="stylesheet" href="css/animation.css" />
<link rel="stylesheet" href="css/properties-editor.css" />
<script src="js/cash-8.1.4.min.js"></script>
<script src="js/qrcode.min.js"></script>
</head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-MYXQ3EPJND"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-MYXQ3EPJND');
</script>
<style>
.close-td {
font-size: 150%;
text-align: right;
padding-right: 10px;
vertical-align: top;
}
.config-table {
border-collapse: collapse;
border: 1px solid black;
}
.config-table td,
.config-table th {
border-left: 1px solid black;
border-right: 1px solid black;
padding: 2px 15px;
}
#post-status {
margin-left: 10px;
}
</style>
<body>
<h1>
EncDec Lib Demo
<span id="about">
<a href="https://github.com/opoto/properties-editor/blob/master/encdec.md">
<i class="icon-github-circled" title="Link to GitHub project"></i>
</a>
</span>
</h1>
<p>
This page shows how to use the <a href="https://github.com/opoto/properties-editor/blob/master/encdec.md">EncDec</a>
Javascript library.
</p>
<table>
<tr>
<td>Clear text</td>
<td><textarea id="txt" cols="120" rows="5">This is my secret message</textarea></td>
</tr>
<tr>
<td>
Password(s)
</td>
<td>
<table class="config-table">
<tr>
<th>Size</th>
<th>Numbers</th>
<th>Alphabet</th>
<th>Symbols</th>
<th>Ambiguous chars</th>
</tr>
<tr>
<td>
<select id="pwd-sz">
<option value="8">8</option>
<option value="10">10</option>
<option value="12" selected>12</option>
<option value="14">14</option>
<option value="16">16</option>
<option value="18">18</option>
<option value="20">20</option>
</select>
</td>
<td>
<label class="noselect"><input type="checkbox" id="pwd-num" checked /> [0-9]</label>
</td>
<td>
<label class="noselect"><input type="checkbox" id="pwd-alpha" checked /> [a-zA-Z]</label>
</td>
<td>
<label class="noselect"><input type="checkbox" id="pwd-sym" /> (,.!?;:&=+-*/_)</label>
</td>
<td>
<label class="noselect"><input type="checkbox" id="pwd-ambig" /> (1iIlL0oO)</label>
</td>
</tr>
</table>
<p>
<label>Number of passwords:
<select id="pwd-number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</label>
(All passwords are combined to encrypt / decrypt)
</p>
</td>
</tr>
<tr>
<td>
<button id="genPwd">Generate</button>
</td>
<td>
<input id="pwd1" class="pwd" type="text" value="" />
<input id="pwd2" class="pwd" type="text" value="" />
<input id="pwd3" class="pwd" type="text" value="" />
(Indicative strength: <span id="pwd-strength"></span>)
</td>
</tr>
<tr>
<td>
Key derivation
</td>
<td>
<table class="config-table">
<tr>
<th>Iterations</th>
<th>Digest</th>
<th>Key Size</th>
</tr>
<tr>
<td>
<select id="enc-iter">
<option value="10">10</option>
<option value="100">100</option>
<option value="1000">1000</option>
<option value="10000">10000</option>
<option value="100000" selected>100000</option>
<option value="1000000">1000000</option>
<option value="10000000">10000000</option>
</select>
</td>
<td>
<select id="enc-algo">
<option value="SHA-1">SHA-1</option>
<option value="SHA-256" selected>SHA-256</option>
</select>
</td>
<td>
<select id="enc-key-size">
<option value="128">128</option>
<option value="256" selected>256</option>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<button id="encBtn">Encrypt</button>
<span>
<i id="cipher-copy" class="icon-clone cipher-copy clickable" title="Copy cipher text to clipboard"></i>
<i class="icon-check cipher-copy hidden" title="Copied"></i>
</span>
</td>
<td><textarea id="enc" cols="120" rows="5"></textarea></td>
</tr>
<tr>
<td>
<button id="post-button" title="Upload encrypted text to temporary cloud storage">Upload</button>
<i id="post-helpButton" class="icon-help-circled clickable"></i>
</td>
<td>
<input id="post-fileurl" type="text" size="30" class="hidden post-result"></span>
<i id="post-fileurl-copy" class="icon-clone post-result post-fileurl-copy clickable"
title="Copy URL to clipboard"></i>
<i class="icon-check post-fileurl-copy hidden" title="Copied"></i>
<i id="post-qrcode-show" class="icon-qrcode clickable" title="Show as QR code"></i>
<span id="post-status"></span>
<br>
</td>
</tr>
<tr id="post-help" class="hidden">
<td class="close-td">
<span id="post-help-close" class="clickable" title="Hide help">⨯</span>
</td>
<td>
Uploads the encrypted output to <a href="https://dpaste.com">https://dpaste.com</a>
temporary cloud storage, and provides the URL and QR Code to retrieve it.
The QR code also includes the last password element in the URL fragment.<br>
By default, the file is stored during 1 day, after which it is deleted.
You can extend this default by setting your expected number of days in
the "dpaste.days" item of your browser's localStorage, e.g.:<br>
<pre>localStorage.setItem("dpaste.days","10")</pre>
You can also set your DPaste API token in "dpaste.apiToken" localStorage,
so that you can list and delete your files from the DPaste web site.
</td>
</tr>
<tr id="post-qrcode-tr">
<td class="close-td">
<span id="post-qrcode-hide" class="clickable" title="Hide QR code">⨯</span>
</td>
<td>
<div id="post-qrcode" alt="QR code"></div>
</td>
</tr>
<tr>
<td><button id="decBtn">Decrypt</button></td>
<td><textarea id="dec" cols="120" rows="5"></textarea></td>
</tr>
</table>
<div id="log"></div>
<div class="footnote">This page collects anonymous usage statistics. Encrypted and clear texts do
not
leave your browser, unless you explicitly upload or share it. Passwords are never shared, except
when you scan or share the QR code.</div>
<script src="js/encdec.js"></script>
<script>
// Initialize library
let encdec;
try {
encdec = new EncDec();
$("#encBtn").on("click", doEncrypt);
$("#decBtn").on("click", doDecrypt);
$("#genPwd").on("click", newPassword);
$("#pwd-number").on("change", readConfig);
$(".pwd").on("keyup", () => { estimatePasswordStrength(readPassword()); });
} catch (error) {
$("#log").append("<p>ERROR: crypto is not supported</p>");
$("#encBtn").attr("disabled", "disabled");
$("#decBtn").attr("disabled", "disabled");
}
let pwdConfig = {};
let pwdSize;
let pwdNumber;
let encConfig = {};
function readConfig() {
pwdNumber = Number.parseInt($("#pwd-number").val());
$("#pwd2").toggle(pwdNumber > 1);
$("#pwd3").toggle(pwdNumber > 2);
pwdSize = Number.parseInt($("#pwd-sz").val());
pwdConfig.size = pwdSize * pwdNumber;
pwdConfig.withNum = $("#pwd-num").is(":checked");
pwdConfig.withAlpha = $("#pwd-alpha").is(":checked");
pwdConfig.withSymbols = $("#pwd-sym").is(":checked");
pwdConfig.allowAmbiguous = $("#pwd-ambig").is(":checked");
encConfig = {};
encConfig.iterations = Number.parseInt($("#enc-iter").val());
encConfig.algorithm = $("#enc-algo").val();
encConfig.keySize = Number.parseInt($("#enc-key-size").val());
console.debug("Password config: " + JSON.stringify(pwdConfig));
console.debug("Crypto config: " + JSON.stringify(encConfig));
}
const passwordStrengths = ["Weak", "Medium", "Strong", "Excellent"];
function estimatePasswordStrength(password) {
const strength = encdec.getPasswordStrength(password);
$("#pwd-strength").text(strength.score + " - " + passwordStrengths[strength.level]);
}
let lastPassword;
function readPassword() {
lastPassword = $("#pwd1").val().trim();
let pwd = lastPassword;
if (pwdNumber > 1) {
lastPassword = $("#pwd2").val().trim();
pwd += lastPassword;
}
if (pwdNumber > 2) {
lastPassword = $("#pwd3").val().trim();
pwd += lastPassword;
}
//console.debug("Password: " + pwd);
estimatePasswordStrength(pwd);
return pwd;
}
async function doEncrypt() {
readConfig();
const txt = $("#txt").val(),
pwd = readPassword();
if (!pwd) {
alert("Enter or generate a password");
return;
}
$("#log").html("");
$("#enc").val("");
clearPost();
try {
cipher = await encdec.aesGcmEncrypt(txt, pwd, encConfig);
$("#enc").val(cipher.encoded);
} catch (err) {
$("#log").append("<p>ERROR: " + err + "</p>");
}
}
async function doDecrypt() {
readConfig();
const enc = $("#enc").val(),
pwd = readPassword();
$("#log").html("");
$("#dec").val("");
try {
plaintext = await encdec.aesGcmDecrypt(enc, pwd);
$("#dec").val(plaintext.decoded);
} catch (err) {
$("#log").append("<p>ERROR: " + err + "</p>");
}
}
$("#cipher-copy").on("click", () => {
$(".cipher-copy").toggle();
try {
navigator.clipboard.writeText($("#enc").val());
} catch (err) {
console.error(err);
}
setTimeout(() => $(".cipher-copy").toggle(), 1500);
});
async function newPassword() {
readConfig();
const password = encdec.generatePassword(pwdConfig);
if (!password) {
alert("Check at least one set of characters in the password generator options");
return;
}
estimatePasswordStrength(password);
let index = 0;
while (index < pwdNumber) {
const pwd = password.substr(index * pwdSize, pwdSize);
index++;
$("#pwd" + index).val(pwd);
}
}
readConfig();
// ============= POST FILE TO CLOUD
function copyFileURL() {
$("#post-fileurl")[0].removeAttribute("disabled");
$("#post-fileurl")[0].focus();
$("#post-fileurl")[0].select();
document.execCommand('copy');
$("#post-fileurl")[0].setAttribute("disabled", true);
$("#post-status").text("URL copied to clipboard");
}
$("#post-fileurl-copy").on("click", () => {
$(".post-fileurl-copy").toggle();
copyFileURL();
setTimeout(() => {
$("#post-status").text("");
$(".post-fileurl-copy").toggle();
}, 2000);
});
$("#post-button").on("click", async () => {
$("#post-button")[0].setAttribute("disabled", true);
clearPost();
$("#post-status").html('<i class="icon-spin6 animate-spin"></i> Wait while uploading...');
const enc = $("#enc").val();
let days = localStorage.getItem("dpaste.days");
days = days ? days : 1;
const post = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: new URLSearchParams({
'expiry_days': days,
'content': enc
})
};
const apiToken = localStorage.getItem("dpaste.apiToken");
if (apiToken) {
post.headers["Authorization"] = "Bearer " + apiToken;
}
try {
let fileUrl;
if (localStorage.getItem("dev.debug") == "true") {
//return;
fileUrl = "https://dpaste.com/DEBUGGING.txt";
} else {
const response = await fetch("https://dpaste.com/api/v2/", post);
fileUrl = await response.text() + ".txt";
}
$(".post-result").show();
$("#post-fileurl").val(fileUrl);
// select & copy to clipboard
copyFileURL();
// display QR Code
new QRCode($("#post-qrcode")[0], {
text: fileUrl + "#" + lastPassword,
width: 200,
height: 200,
correctLevel: QRCode.CorrectLevel.M
});
$("#post-qrcode-show").show();
$("#post-help").hide();
// done
} catch (error) {
$("#post-status").text(error);
}
$("#post-button")[0].removeAttribute("disabled");
setTimeout(() => $("#post-status").text(""), 2000);
});
function toggleQRcode() {
$("#post-qrcode-show").toggle();
$("#post-qrcode-tr").toggle();
}
$("#post-qrcode-show, #post-qrcode-hide").on("click", toggleQRcode);
toggleQRcode();
function clearPost() {
$("#post-fileurl").text("");
$(".post-result, #post-qrcode-tr, #post-qrcode-show").hide();
$("#post-qrcode").empty();
}
$("#post-helpButton, #post-help-close").on("click", () => {
$("#post-help").toggle();
$("#post-helpButton").toggle();
});
clearPost();
</script>
</body>
</html>