MCPcopy Create free account
hub / github.com/ProtonDriveApps/sdk / encryptCacheValue

Function encryptCacheValue

cli/src/cache/crypto.ts:9–20  ·  view source on GitHub ↗
(cacheKey: string, plaintext: string, ikm: Buffer)

Source from the content-addressed store, hash-verified

7const KEY_BYTE_COUNT = 32;
8
9export 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
22export function decryptCacheValue(cacheKey: string, encryptedBase64: string, ikm: Buffer): string {
23 const combined = Buffer.from(encryptedBase64, 'base64');

Callers 1

setEntityMethod · 0.90

Calls 3

concatBuffersFunction · 0.85
updateMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected