-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.js
45 lines (42 loc) · 1.73 KB
/
crypto.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
34
35
36
37
38
39
40
41
42
43
44
45
import { decode as hexdecode } from "https://deno.land/std@0.130.0/encoding/hex.ts";
import { encode as hexencode } from "https://deno.land/std@0.130.0/encoding/hex.ts";
var crypto = (key) => {
if (key.length != 32) {
throw new Error("crypto key must be 32 length");
return;
}
return {
encrypt: async (k, v) => {
var text = JSON.stringify({
k: k,
v: v,
t: parseInt(Date.now() / 1000),
});
var iv = window.crypto.getRandomValues(new Uint8Array(16));
var ab = await window.crypto.subtle.encrypt(
{
name: "AES-CBC",
iv,
},
await window.crypto.subtle.importKey("raw", new TextEncoder().encode(key).buffer, "AES-CBC", false, ["encrypt", "decrypt"]),
new TextEncoder().encode(text)
);
return new TextDecoder().decode(hexencode(new Uint8Array([...iv, ...new Uint8Array(ab)])));
},
decrypt: async (k, c, lifecycle) => {
var b = hexdecode(new TextEncoder().encode(c));
var ab = await window.crypto.subtle.decrypt({ name: "AES-CBC", iv: b.slice(0, 16) }, await window.crypto.subtle.importKey("raw", new TextEncoder().encode(key).buffer, "AES-CBC", false, ["encrypt", "decrypt"]), b.slice(16));
var b = new Uint8Array(ab);
var s = new TextDecoder().decode(b);
var o = JSON.parse(s);
if (lifecycle && o.t + lifecycle < parseInt(Date.now() / 1000)) {
return null;
}
if (o.k !== k) {
return null;
}
return o.v;
},
};
};
export default crypto;