-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
example-encryption.js
44 lines (35 loc) · 983 Bytes
/
example-encryption.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
import crypto from "crypto";
import { StringEncrypter } from "@jmondi/string-encrypt-decrypt";
async function createKey() {
const cryptoKey = await crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"],
);
const exportedKey = await crypto.subtle.exportKey("jwk", cryptoKey);
return JSON.stringify(exportedKey);
}
async function createEncryptedString(cryptoString) {
const stringEncrypter = await StringEncrypter.fromCryptoString(cryptoString);
const encryptedString = await stringEncrypter.encrypt(
JSON.stringify({
url: "https://jasonraimondi.com",
isFullHeight: true,
forceReload: true,
}),
);
return encryptedString;
}
async function main() {
const CRYPTO_KEY = await createKey();
const hash = await createEncryptedString(CRYPTO_KEY);
return {
CRYPTO_KEY,
exampleUrl: `http://localhost:3039/?hash=${hash}`,
hash,
};
}
main().then(console.log);