import crypto from "node:crypto"; import { createImplementation } from "@webiny/feature/api"; import { Encryption as EncryptionAbstraction } from "./abstractions.js"; import { EncryptionKeyCache } from "./abstractions.js"; import { EncryptionConfig } from "./abstractions.js"; function keyLengthForAlgorithm(algorithm: string): number { // Parse key size in bits from algorithm name (e.g. "aes-256-gcm" → 32 bytes). const bits = parseInt(algorithm.split("-")[1]); if (isNaN(bits)) { throw new Error(`Cannot determine key length from algorithm "${algorithm}".`); } return bits / 8; } export class EncryptionImpl implements EncryptionAbstraction.Interface { private key: Buffer | null; private algorithm: string; constructor( config: EncryptionConfig.Interface, // Registered in the root container, so the derived key outlives the request. Without it (in // tests, for example) the key is derived for every instance. keyCache: EncryptionKeyCache.Interface | undefined ) { this.algorithm = config.algorithm; if (!config.passphrase) { this.key = null; return; } // Salt is optional. It ensures two projects using the same passphrase derive different keys, // but since passphrases should be unique per project anyway, it's extra insurance rather than a necessity. const keyLength = keyLengthForAlgorithm(this.algorithm); if (keyCache) { this.key = keyCache.getOrDerive(config.passphrase, config.salt, keyLength); } else { this.key = crypto.scryptSync(config.passphrase, config.salt, keyLength); } } async encrypt(value: string): Promise { if (!this.key) { return value; } const iv = crypto.randomBytes(12); const cipher = crypto.createCipheriv(this.algorithm, this.key, iv); const encrypted = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]); const authTag = (cipher as crypto.CipherGCM).getAuthTag(); return Buffer.concat([iv, authTag, encrypted]).toString("base64"); } async decrypt(value: string): Promise { if (!this.key) { return value; } const data = Buffer.from(value, "base64"); const iv = data.subarray(0, 12); const authTag = data.subarray(12, 28); const ciphertext = data.subarray(28); const decipher = crypto.createDecipheriv(this.algorithm, this.key, iv); (decipher as crypto.DecipherGCM).setAuthTag(authTag); return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString("utf8"); } } export const Encryption = createImplementation({ abstraction: EncryptionAbstraction, implementation: EncryptionImpl, dependencies: [EncryptionConfig, [EncryptionKeyCache, { optional: true }]] });