Repository navigation
Expand file tree
/
Copy pathWebCryptAsymFullCoverage.test.js
More file actions
91 lines (72 loc) · 3.73 KB
/
Copy pathWebCryptAsymFullCoverage.test.js
File metadata and controls
91 lines (72 loc) · 3.73 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// __tests__/WebCryptAsymFullCoverage.test.js
import { WebCryptAsym } from "../src/WebCryptAsym.js";
describe("WebCryptAsym Full Coverage Expansion", () => {
let wca;
let rsaKeyPair;
let ecdsaKeyPair;
beforeAll(async () => {
wca = new WebCryptAsym();
rsaKeyPair = await wca.generateKeyPair();
ecdsaKeyPair = await wca.generateSigningKeyPair("P-256");
});
test("ECDSA P-256 and P-384 digital signature signing & verification", async () => {
const data = "Message to be digitally signed with ECDSA";
// P-256
const signatureP256 = await wca.signText(data, ecdsaKeyPair.privateKey);
expect(typeof signatureP256).toBe("string");
const validP256 = await wca.verifyText(data, signatureP256, ecdsaKeyPair.publicKey);
expect(validP256).toBe(true);
// P-384
const ecdsaP384KeyPair = await wca.generateSigningKeyPair("P-384");
const signatureP384 = await wca.signText(data, ecdsaP384KeyPair.privateKey);
const validP384 = await wca.verifyText(data, signatureP384, ecdsaP384KeyPair.publicKey);
expect(validP384).toBe(true);
});
test("Key export and import (spki / pkcs8)", async () => {
const publicB64 = await wca.exportPublicKey(rsaKeyPair.publicKey);
expect(typeof publicB64).toBe("string");
const privateB64 = await wca.exportPrivateKey(rsaKeyPair.privateKey);
expect(typeof privateB64).toBe("string");
const importedPublic = await wca.importPublicKey(publicB64);
expect(importedPublic).toBeDefined();
const importedPrivate = await wca.importPrivateKey(privateB64);
expect(importedPrivate).toBeDefined();
});
test("ECDH Key Agreement & Encryption (generateECDHKeyPair, deriveECDHSharedSecret, encryptWithECDH)", async () => {
const aliceKeys = await wca.generateECDHKeyPair("P-256");
const bobKeys = await wca.generateECDHKeyPair("P-256");
const sharedKeyAlice = await wca.deriveECDHSharedSecret(
aliceKeys.privateKey,
bobKeys.publicKey
);
const sharedKeyBob = await wca.deriveECDHSharedSecret(bobKeys.privateKey, aliceKeys.publicKey);
expect(sharedKeyAlice).toBeDefined();
expect(sharedKeyBob).toBeDefined();
const message = "Secret ECDH payload";
const encrypted = await wca.encryptWithECDH(message, aliceKeys.privateKey, bobKeys.publicKey);
const decrypted = await wca.decryptWithECDH(encrypted, bobKeys.privateKey, aliceKeys.publicKey);
expect(decrypted).toBe(message);
});
test("JWE compact encryption and decryption (RFC 7516)", async () => {
const payload = JSON.stringify({ user: "Alice", role: "admin" });
const jweCompact = await wca.encryptJWE(payload, rsaKeyPair.publicKey);
expect(typeof jweCompact).toBe("string");
expect(jweCompact.split(".").length).toBe(5);
const decrypted = await wca.decryptJWE(jweCompact, rsaKeyPair.privateKey);
const decryptedStr = typeof decrypted === "object" ? JSON.stringify(decrypted) : decrypted;
expect(JSON.parse(decryptedStr)).toEqual(JSON.parse(payload));
});
test("Asymmetric WebRTC stream transforms (createEncryptTransform & createDecryptTransform)", async () => {
const encryptTransform = await wca.createEncryptTransform(rsaKeyPair.publicKey);
const decryptTransform = await wca.createDecryptTransform(rsaKeyPair.privateKey);
const rawData = new Uint8Array([1, 2, 3, 4, 5]).buffer;
const frame = { data: rawData };
const enqueued = [];
await encryptTransform(frame, { enqueue: f => enqueued.push(f) });
expect(enqueued.length).toBe(1);
const decryptedEnqueued = [];
await decryptTransform({ data: enqueued[0].data }, { enqueue: f => decryptedEnqueued.push(f) });
expect(decryptedEnqueued.length).toBe(1);
expect(new Uint8Array(decryptedEnqueued[0].data)).toEqual(new Uint8Array([1, 2, 3, 4, 5]));
});
});