import { describe, expect, it } from "bun:test"; describe("Web Crypto", () => { it("has globals", () => { expect(crypto.subtle !== undefined).toBe(true); expect(CryptoKey.name).toBe("CryptoKey"); expect(SubtleCrypto.name).toBe("SubtleCrypto"); }); 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); }); }); n> Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/test/fixtures/mdx-namespace (unfollow)
AgeCommit message (Collapse)AuthorFilesLines
2023-07-11Filter out astro from `peerDependencies` in `astro add` (#7620)Gravatar Chris Swithinbank 2-1/+7
2023-07-11[ci] formatGravatar bluwy 1-1/+1
2023-07-11Refactor simple CLI commands (#7619)Gravatar Bjorn Lu 12-115/+123
2023-07-10fix: `astro info` command fallback for package manager (#7618)Gravatar Emanuele Stoppa 2-1/+7
2023-07-10Add CLI startup speed benchmark (#7617)Gravatar Bjorn Lu 5-11/+95
2023-07-10[ci] formatGravatar bluwy 1-1/+1