* Helper: encrypt a string using the same RSA+AES envelope scheme * used by the shared lib (src/lib/encryption.ts). This allows us * to test decryption without importing the shared encryption module.
(value: string, publicKeyPem: string)
| 16 | * to test decryption without importing the shared encryption module. |
| 17 | */ |
| 18 | function encryptForTest(value: string, publicKeyPem: string): EncryptedEnvelope { |
| 19 | const dek = randomBytes(32); |
| 20 | const iv = randomBytes(16); |
| 21 | const cipher = createCipheriv('aes-256-gcm', dek, iv); |
| 22 | let encrypted = cipher.update(value, 'utf8'); |
| 23 | encrypted = Buffer.concat([encrypted, cipher.final()]); |
| 24 | const authTag = cipher.getAuthTag(); |
| 25 | const encryptedDataBuffer = Buffer.concat([iv, encrypted, authTag]); |
| 26 | const encryptedDEKBuffer = publicEncrypt( |
| 27 | { key: publicKeyPem, padding: constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, |
| 28 | dek |
| 29 | ); |
| 30 | return { |
| 31 | encryptedData: encryptedDataBuffer.toString('base64'), |
| 32 | encryptedDEK: encryptedDEKBuffer.toString('base64'), |
| 33 | algorithm: 'rsa-aes-256-gcm', |
| 34 | version: 1, |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | describe('encryption utilities', () => { |
| 39 | let publicKey: string; |
no test coverage detected