-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.html
94 lines (76 loc) · 3 KB
/
encrypt.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form id="encrypt">
<label for="file">File to encrypt:</label>
<input type="file" id="file" required />
<br />
<label for="password">Encryption password:</label>
<input type="password" id="password" minlength="1" />
<br />
<label for="iterations">Hash iterations:</label>
<input type="number" id="iterations" value="10000" min="1" max="4294967296" />
<br />
<input type="checkbox" id="base64" />
<label for="base64">Get base64-encoded contents</label>
<br />
<input type="submit" value="Encrypt" />
<input type="reset" value="Clear" />
</form>
<div id="results" style="display: none">
<a id="bytes">Encrypted file</a>
<pre><code id="params"></code></pre>
</div>
<div id="error" style="display: none"></div>
<script type="module">
import { encrypt } from "./static-secret.js";
document.forms.encrypt.onreset = () => {
const resultsElt = document.querySelector("#results");
const errorElt = document.querySelector("#error");
resultsElt.style.display = "none";
errorElt.style.display = "none";
const bytesElt = resultsElt.querySelector("#bytes");
if (bytesElt.href) URL.revokeObjectURL(bytesElt.href);
bytesElt.href = "";
};
document.forms.encrypt.onsubmit = async (event) => {
event.preventDefault();
const form = event.target;
const resultsElt = document.querySelector("#results");
const errorElt = document.querySelector("#error");
const dataFile = form.elements.file.files[0];
const password = form.elements.password.value;
const iterations = Number(form.elements.iterations.value);
const needsBase64 = form.elements.base64.checked;
resultsElt.style.display = "none";
errorElt.style.display = "none";
try {
const { bytes, params } = await encrypt(dataFile, password, {
iterations,
});
const bytesElt = resultsElt.querySelector("#bytes");
if (bytesElt.href) URL.revokeObjectURL(bytesElt.href);
const encryptedFile = new File([bytes], dataFile.name + ".encrypted", {
type: "application/octet-stream",
});
bytesElt.href = URL.createObjectURL(encryptedFile);
const paramsElt = resultsElt.querySelector("#params");
paramsElt.innerText = `data-static-secret="${params}"`;
if (needsBase64) {
const base64 = btoa(String.fromCharCode(...bytes));
paramsElt.innerHTML += `\nbase64 contents for embedding:\n` + base64;
}
resultsElt.style.display = "unset";
} catch (error) {
console.error(error);
errorElt.innerText = String(error);
errorElt.style.display = "unset";
}
};
</script>
</body>
</html>