(salt: BufferSource)
| 33 | }; |
| 34 | |
| 35 | async function deriveKey(salt: BufferSource): Promise<CryptoKey> { |
| 36 | const key = ENCRYPTION_KEY(); |
| 37 | if (!key) throw new Error("Encryption key is not available"); |
| 38 | |
| 39 | const keyBuffer = Buffer.from(key, "hex"); |
| 40 | |
| 41 | const keyMaterial = await crypto.subtle.importKey( |
| 42 | "raw", |
| 43 | keyBuffer, |
| 44 | "PBKDF2", |
| 45 | false, |
| 46 | ["deriveKey"], |
| 47 | ); |
| 48 | |
| 49 | return crypto.subtle.deriveKey( |
| 50 | { |
| 51 | name: "PBKDF2", |
| 52 | salt, |
| 53 | iterations: ITERATIONS, |
| 54 | hash: "SHA-256", |
| 55 | }, |
| 56 | keyMaterial, |
| 57 | ALGORITHM, |
| 58 | false, |
| 59 | ["encrypt", "decrypt"], |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | export async function encrypt(text: string): Promise<string> { |
| 64 | if (!text) { |
no test coverage detected