-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (52 loc) · 1.72 KB
/
Copy pathindex.js
File metadata and controls
62 lines (52 loc) · 1.72 KB
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
// need to build with `wasm-pack build --target web`
// check vite.config.ts as well
import init, * as ecies from "ecies-wasm";
import "./style.css";
import { bytesToHex } from "./utils";
let sk, pk;
init().then(() => {
[sk, pk] = ecies.generateKeypair();
});
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const text = "hello ecies-wasm🔒";
export function setup(encryptedElement, textElement, decryptedElement) {
let encrypted;
encryptedElement.innerHTML = "click me to encrypt";
textElement.innerHTML = text;
decryptedElement.innerHTML = "click me to decrypt";
const _encrypt = () => {
encrypted = ecies.encrypt(pk, encoder.encode(text));
encryptedElement.innerHTML = "encrypted:";
textElement.innerHTML = `<code>${bytesToHex(encrypted)}</code>`;
decryptedElement.innerHTML = "click me to decrypt";
};
const _decrypt = () => {
encryptedElement.innerHTML = "click me to encrypt";
if (encrypted) {
const decrypted = decoder.decode(ecies.decrypt(sk, encrypted));
textElement.innerHTML = `${decrypted}`;
decryptedElement.innerHTML = "decrypted:";
encrypted = undefined;
} else {
textElement.innerHTML = "click encrypt button first";
}
};
encryptedElement.addEventListener("click", () => _encrypt());
decryptedElement.addEventListener("click", () => _decrypt());
}
document.querySelector("#app").innerHTML = `
<div>
<h1>Hello ecies-wasm!</h1>
<div class="card">
<button id="encrypted" type="button"></button>
<button id="decrypted" type="button"></button>
</div>
<p id="text"></p>
</div>
`;
setup(
document.querySelector("#encrypted"),
document.querySelector("#text"),
document.querySelector("#decrypted")
);