-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
46 lines (42 loc) · 1.03 KB
/
main.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
46
import { aes256cbc, aes256gcm } from "@ecies/ciphers/aes";
import { xchacha20 } from "@ecies/ciphers/chacha";
import { randomBytes } from "@noble/ciphers/webcrypto";
const TEXT = "hello world🌍!";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const msg = encoder.encode(TEXT);
const ciphers = [
{
keyLength: 32,
nonceLength: 16,
callback: aes256gcm,
aad: randomBytes(16),
},
{
keyLength: 32,
nonceLength: 12,
callback: aes256gcm,
aad: randomBytes(16),
},
{
keyLength: 32,
nonceLength: 16,
callback: aes256cbc,
aad: undefined,
},
{
keyLength: 32,
nonceLength: 24,
callback: xchacha20,
aad: randomBytes(16),
},
];
for (const { keyLength, nonceLength, callback, aad } of ciphers) {
const key = randomBytes(keyLength);
const nonce = randomBytes(nonceLength);
const cipher = callback(key, nonce, aad);
console.log(
`${callback.name} (nonce length ${nonce.length}) decrypted:`,
decoder.decode(cipher.decrypt(cipher.encrypt(msg)))
);
}