-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathasymmetric.html
More file actions
67 lines (56 loc) · 2.6 KB
/
Copy pathasymmetric.html
File metadata and controls
67 lines (56 loc) · 2.6 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
63
64
65
66
67
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>WebCryptAsym — Asymmetric Example</title>
</head>
<body>
<h1>WebCryptAsym — RSA Encryption & ECDSA Signing</h1>
<p>Generate an RSA keypair for encrypt/decrypt, and an ECDSA keypair for sign/verify.</p>
<button id="gen">Generate Key Pairs</button>
<button id="encrypt">Encrypt & Decrypt</button>
<button id="sign">Sign & Verify</button>
<pre id="out"></pre>
<script type="module">
// For npm users: import { WebCryptAsym } from 'webcrypt';
import { WebCryptAsym } from "../src/WebCryptAsym.js";
const asym = new WebCryptAsym();
const out = document.getElementById("out");
let rsaKeys, sigKeys;
document.getElementById("gen").addEventListener("click", async () => {
out.textContent = "Generating RSA-4096 key pair (this may take a moment)...";
rsaKeys = await asym.generateKeyPair();
const pubB64 = await asym.exportPublicKey(rsaKeys.publicKey);
out.textContent = `RSA Public Key (SPKI, base64):\n${pubB64}\n`;
out.textContent += "\nGenerating ECDSA signing key pair...";
sigKeys = await asym.generateSigningKeyPair("P-256");
out.textContent += `\nECDSA Public Key (base64):\n${sigKeys.publicKeyB64}`;
});
document.getElementById("encrypt").addEventListener("click", async () => {
if (!rsaKeys) {
out.textContent = "Generate keys first.";
return;
}
const message = "Hello from WebCryptAsym!";
out.textContent = `Encrypting: "${message}"...\n`;
const encrypted = await asym.encryptText(message, rsaKeys.publicKey);
out.textContent += `Encrypted (base64): ${encrypted.slice(0, 60)}...\n`;
const decrypted = await asym.decryptText(encrypted, rsaKeys.privateKey);
out.textContent += `Decrypted: "${decrypted}"`;
});
document.getElementById("sign").addEventListener("click", async () => {
if (!sigKeys) {
out.textContent = "Generate keys first.";
return;
}
const message = "I approve transaction #123";
out.textContent = `Signing: "${message}"...\n`;
const signature = await asym.signText(message, sigKeys.privateKey);
out.textContent += `Signature (base64): ${signature}\n`;
const valid = await asym.verifyText(message, signature, sigKeys.publicKey);
out.textContent += valid ? "Signature valid ✅" : "Signature invalid ❌";
});
</script>
</body>
</html>