diff options
Diffstat (limited to 'test/bun.js/web-crypto.test.ts')
-rw-r--r-- | test/bun.js/web-crypto.test.ts | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/bun.js/web-crypto.test.ts b/test/bun.js/web-crypto.test.ts new file mode 100644 index 000000000..22f69f43b --- /dev/null +++ b/test/bun.js/web-crypto.test.ts @@ -0,0 +1,83 @@ +import { describe, expect, it } from "bun:test"; + +describe("Web Crypto", () => { + it("has globals", () => { + expect(crypto.subtle !== undefined).toBe(true); + expect(CryptoKey !== undefined).toBe(true); + expect(SubtleCrypto !== undefined).toBe(true); + }); + it("should encrypt and decrypt", async () => { + const key = await crypto.subtle.generateKey( + { + name: "AES-GCM", + length: 256, + }, + true, + ["encrypt", "decrypt"] + ); + const iv = crypto.getRandomValues(new Uint8Array(12)); + const data = new TextEncoder().encode("Hello World!"); + const encrypted = await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv, + }, + key, + data + ); + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv, + }, + key, + encrypted + ); + expect(new TextDecoder().decode(decrypted)).toBe("Hello World!"); + }); + + it("should verify and sign", async () => { + async function importKey(secret) { + return await crypto.subtle.importKey( + "raw", + new TextEncoder().encode(secret), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign", "verify"] + ); + } + + async function signResponse(message, secret) { + const key = await importKey(secret); + const signature = await crypto.subtle.sign( + "HMAC", + key, + new TextEncoder().encode(message) + ); + + // Convert ArrayBuffer to Base64 + return btoa(String.fromCharCode(...new Uint8Array(signature))); + } + + async function verifySignature(message, signature, secret) { + const key = await importKey(secret); + + // Convert Base64 to Uint8Array + const sigBuf = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); + + return await crypto.subtle.verify( + "HMAC", + key, + sigBuf, + new TextEncoder().encode(message) + ); + } + + const msg = `hello world`; + const SECRET = "secret"; + const signature = await signResponse(msg, SECRET); + + const isSigValid = await verifySignature(msg, signature, SECRET); + expect(isSigValid).toBe(true); + }); +}); |