Something went wrong. Try again.
Fork of backnotprop/plannotator with reduced surface area
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798/** * AES-256-GCM encryption for zero-knowledge paste storage. * * Uses Web Crypto API — works in browsers, Bun, and edge runtimes. * The key never leaves the client; it lives in the URL fragment. */
/** * Encrypt a compressed base64url string with a fresh AES-256-GCM key. * * Returns { ciphertext, key } where: * - ciphertext: base64url-encoded (12-byte IV prepended to GCM output) * - key: base64url-encoded 256-bit key for the URL fragment */export async function encrypt( compressedData: string): Promise<{ ciphertext: string; key: string }> { const cryptoKey = await crypto.subtle.generateKey( { name: 'AES-GCM', length: 256 }, true, ['encrypt'] );
const iv = crypto.getRandomValues(new Uint8Array(12)); const plaintext = new TextEncoder().encode(compressedData);
const encrypted = await crypto.subtle.encrypt( { name: 'AES-GCM', iv }, cryptoKey, plaintext );
// Prepend IV to ciphertext (IV || ciphertext+tag) const combined = new Uint8Array(iv.length + encrypted.byteLength); combined.set(iv, 0); combined.set(new Uint8Array(encrypted), iv.length);
const rawKey = await crypto.subtle.exportKey('raw', cryptoKey);
return { ciphertext: bytesToBase64url(combined), key: bytesToBase64url(new Uint8Array(rawKey)), };}
/** * Decrypt a ciphertext string using a base64url-encoded AES-256-GCM key. * * Expects ciphertext format: base64url(IV || encrypted+tag) * Returns the original compressed base64url string. */export async function decrypt( ciphertext: string, key: string): Promise<string> { const combined = base64urlToBytes(ciphertext); const rawKey = base64urlToBytes(key);
const iv = combined.slice(0, 12); const encrypted = combined.slice(12);
const cryptoKey = await crypto.subtle.importKey( 'raw', rawKey.buffer as ArrayBuffer, { name: 'AES-GCM', length: 256 }, false, ['decrypt'] );
const decrypted = await crypto.subtle.decrypt( { name: 'AES-GCM', iv }, cryptoKey, encrypted );
return new TextDecoder().decode(decrypted);}
// --- Helpers ---
function bytesToBase64url(bytes: Uint8Array): string { // Loop to avoid RangeError on large payloads (same approach as compress.ts) let binary = ''; for (let i = 0; i < bytes.length; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary) .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=/g, '');}
function base64urlToBytes(b64: string): Uint8Array { const base64 = b64.replace(/-/g, '+').replace(/_/g, '/'); const binary = atob(base64); return Uint8Array.from(binary, c => c.charCodeAt(0));}