(cacheKey: string, plaintext: string, ikm: Buffer)
| 7 | const KEY_BYTE_COUNT = 32; |
| 8 | |
| 9 | export function encryptCacheValue(cacheKey: string, plaintext: string, ikm: Buffer): string { |
| 10 | const plaintextBytes = Buffer.from(plaintext, 'utf8'); |
| 11 | const salt = randomBytes(SALT_BYTE_COUNT); |
| 12 | const info = concatBuffers(CACHE_ENCRYPTION_CONTEXT, Buffer.from(cacheKey, 'utf8')); |
| 13 | const derived = Buffer.from(hkdfSync('sha256', ikm, salt, info, KEY_BYTE_COUNT + IV_BYTE_COUNT)); |
| 14 | const aesKey = derived.subarray(0, KEY_BYTE_COUNT); |
| 15 | const iv = derived.subarray(KEY_BYTE_COUNT); |
| 16 | const cipher = createCipheriv('aes-256-gcm', aesKey, iv, { authTagLength: TAG_BYTE_COUNT }); |
| 17 | const ciphertext = Buffer.concat([cipher.update(plaintextBytes), cipher.final()]); |
| 18 | const tag = cipher.getAuthTag(); |
| 19 | return Buffer.concat([salt, ciphertext, tag]).toString('base64'); |
| 20 | } |
| 21 | |
| 22 | export function decryptCacheValue(cacheKey: string, encryptedBase64: string, ikm: Buffer): string { |
| 23 | const combined = Buffer.from(encryptedBase64, 'base64'); |
no test coverage detected