|
| 1 | +/** |
| 2 | + * AES Encryption/Decryption using Web Crypto API (Bun/Node/Browser compatible) |
| 3 | + */ |
| 4 | + |
| 5 | +export interface EncryptOptions { |
| 6 | + algorithm?: 'AES-GCM' | 'AES-CBC' |
| 7 | + keyLength?: 128 | 192 | 256 |
| 8 | + iv?: Uint8Array |
| 9 | +} |
| 10 | + |
| 11 | +export interface EncryptResult { |
| 12 | + encrypted: string |
| 13 | + iv: string |
| 14 | + algorithm: string |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Generate a cryptographic key from a passphrase using PBKDF2 |
| 19 | + */ |
| 20 | +async function deriveKey( |
| 21 | + passphrase: string, |
| 22 | + salt: Uint8Array, |
| 23 | + keyLength: number = 256, |
| 24 | +): Promise<CryptoKey> { |
| 25 | + const encoder = new TextEncoder() |
| 26 | + const passphraseKey = await crypto.subtle.importKey( |
| 27 | + 'raw', |
| 28 | + encoder.encode(passphrase), |
| 29 | + { name: 'PBKDF2' }, |
| 30 | + false, |
| 31 | + ['deriveKey'], |
| 32 | + ) |
| 33 | + |
| 34 | + return await crypto.subtle.deriveKey( |
| 35 | + { |
| 36 | + name: 'PBKDF2', |
| 37 | + salt, |
| 38 | + iterations: 100000, |
| 39 | + hash: 'SHA-256', |
| 40 | + }, |
| 41 | + passphraseKey, |
| 42 | + { name: 'AES-GCM', length: keyLength }, |
| 43 | + false, |
| 44 | + ['encrypt', 'decrypt'], |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Encrypt a message using AES-GCM |
| 50 | + */ |
| 51 | +export async function encrypt( |
| 52 | + message: string, |
| 53 | + passphrase: string, |
| 54 | + options?: EncryptOptions, |
| 55 | +): Promise<EncryptResult> { |
| 56 | + const algorithm = options?.algorithm || 'AES-GCM' |
| 57 | + const keyLength = options?.keyLength || 256 |
| 58 | + const iv = options?.iv || crypto.getRandomValues(new Uint8Array(12)) |
| 59 | + |
| 60 | + // Generate salt for key derivation |
| 61 | + const salt = crypto.getRandomValues(new Uint8Array(16)) |
| 62 | + |
| 63 | + // Derive key from passphrase |
| 64 | + const key = await deriveKey(passphrase, salt, keyLength) |
| 65 | + |
| 66 | + // Encrypt the message |
| 67 | + const encoder = new TextEncoder() |
| 68 | + const encrypted = await crypto.subtle.encrypt( |
| 69 | + { |
| 70 | + name: algorithm, |
| 71 | + iv, |
| 72 | + }, |
| 73 | + key, |
| 74 | + encoder.encode(message), |
| 75 | + ) |
| 76 | + |
| 77 | + // Combine salt + IV + encrypted data |
| 78 | + const combined = new Uint8Array(salt.length + iv.length + encrypted.byteLength) |
| 79 | + combined.set(salt, 0) |
| 80 | + combined.set(iv, salt.length) |
| 81 | + combined.set(new Uint8Array(encrypted), salt.length + iv.length) |
| 82 | + |
| 83 | + // Convert to base64 |
| 84 | + return { |
| 85 | + encrypted: Buffer.from(combined).toString('base64'), |
| 86 | + iv: Buffer.from(iv).toString('base64'), |
| 87 | + algorithm, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Decrypt a message using AES-GCM |
| 93 | + */ |
| 94 | +export async function decrypt( |
| 95 | + encryptedData: string, |
| 96 | + passphrase: string, |
| 97 | + algorithm: string = 'AES-GCM', |
| 98 | +): Promise<string> { |
| 99 | + // Decode from base64 |
| 100 | + const combined = Buffer.from(encryptedData, 'base64') |
| 101 | + |
| 102 | + // Extract salt, IV, and encrypted data |
| 103 | + const salt = combined.slice(0, 16) |
| 104 | + const iv = combined.slice(16, 28) // 12 bytes for GCM |
| 105 | + const encrypted = combined.slice(28) |
| 106 | + |
| 107 | + // Derive key from passphrase |
| 108 | + const key = await deriveKey(passphrase, salt) |
| 109 | + |
| 110 | + // Decrypt the message |
| 111 | + const decrypted = await crypto.subtle.decrypt( |
| 112 | + { |
| 113 | + name: algorithm, |
| 114 | + iv, |
| 115 | + }, |
| 116 | + key, |
| 117 | + encrypted, |
| 118 | + ) |
| 119 | + |
| 120 | + // Convert back to string |
| 121 | + const decoder = new TextDecoder() |
| 122 | + return decoder.decode(decrypted) |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Simple base64 encode (Bun native) |
| 127 | + */ |
| 128 | +export function base64Encode(input: string): string { |
| 129 | + return Buffer.from(input, 'utf-8').toString('base64') |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Simple base64 decode (Bun native) |
| 134 | + */ |
| 135 | +export function base64Decode(input: string): string { |
| 136 | + return Buffer.from(input, 'base64').toString('utf-8') |
| 137 | +} |
0 commit comments