(secret: string)
| 54 | |
| 55 | /** Derive an AES-256-GCM CryptoKey from a secret string using PBKDF2. */ |
| 56 | export async function deriveEncryptionKey(secret: string): Promise<CryptoKey> { |
| 57 | const keyMaterial = await crypto.subtle.importKey( |
| 58 | 'raw', |
| 59 | new TextEncoder().encode(secret), |
| 60 | 'PBKDF2', |
| 61 | false, |
| 62 | ['deriveKey'] |
| 63 | ); |
| 64 | |
| 65 | return crypto.subtle.deriveKey( |
| 66 | { |
| 67 | name: 'PBKDF2', |
| 68 | salt: PBKDF2_SALT, |
| 69 | iterations: PBKDF2_ITERATIONS, |
| 70 | hash: 'SHA-256', |
| 71 | }, |
| 72 | keyMaterial, |
| 73 | { name: ALGORITHM, length: 256 }, |
| 74 | false, |
| 75 | ['encrypt', 'decrypt'] |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | // --- base64 helpers using platform-agnostic approach --- |
| 80 |
no outgoing calls
no test coverage detected