-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny-enc.js
33 lines (33 loc) · 1000 Bytes
/
tiny-enc.js
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
(() => {
const subtle = crypto.subtle;
const te = new TextEncoder();
const ua = ab => new Uint8Array(ab);
const alg = { name: "AES-CBC" };
const fcc = x => String.fromCharCode(x);
const base642ab = b => ua(atob(b).split('').map(c => c.charCodeAt(0)));
const getKey = async pass =>
await subtle.importKey(
"raw",
te.encode((pass.padStart(16, '0').slice(0, 16))),
alg,
false,
["encrypt", "decrypt"]
);
window.tinyEnc = {
encrypt: async (pass, msg) => {
const iv = crypto.getRandomValues(ua(16));
return btoa([...ua(iv), ...ua(await subtle.encrypt(
{ ...alg, iv },
await getKey(pass),
te.encode(msg)
))].reduce((data, byte) => data + fcc(byte), ''));
},
decrypt: async (pass, msg) => {
return [...ua(await subtle.decrypt(
{ ...alg, iv: base642ab(msg).slice(0, 16) },
await getKey(pass),
base642ab(msg).slice(16)
))].map(x => fcc(x)).join('');
}
}
})();