import { describe, expect, test } from "bun:test"; import { x448 } from "@noble/curves/ed448.js"; import { decrypt, encrypt, getSharedSecret } from "../src/crypto"; function toBase64(bytes: Uint8Array): string { if (typeof Buffer !== "undefined") { return Buffer.from(bytes).toString("base64"); } let binary = ""; for (const byte of bytes) { binary += String.fromCharCode(byte); } return btoa(binary); } describe("crypto", () => { test("encrypt/decrypt roundtrip", async () => { const privA = new Uint8Array(56); crypto.getRandomValues(privA); const pubA = x448.getPublicKey(privA); const privB = new Uint8Array(56); crypto.getRandomValues(privB); const pubB = x448.getPublicKey(privB); const sharedA = await getSharedSecret( toBase64(privA), toBase64(pubA), toBase64(pubB), ); const sharedB = await getSharedSecret( toBase64(privB), toBase64(pubB), toBase64(pubA), ); expect(sharedA).toBe(sharedB); const message = "hello tauth sdk"; const cipher = await encrypt(sharedA, message); const plain = await decrypt(sharedB, cipher); expect(plain).toBe(message); }); test("decrypt fails when using wrong shared secret", async () => { const secretA = "01".repeat(56); const secretB = "02".repeat(56); const cipher = await encrypt(secretA, "sensitive"); await expect(decrypt(secretB, cipher)).rejects.toThrow(); }); test("getSharedSecret rejects malformed key material", async () => { await expect(getSharedSecret("AA==", "AA==", "AA==")).rejects.toThrow( "not a valid X448", ); }); });