{"version":3,"file":"CryptoWrapper.cjs","sources":["../../../../../packages/engine-http/src/utils/CryptoWrapper.ts"],"sourcesContent":["import * as crypto from 'node:crypto'\n\nexport class CryptoWrapper {\n\tpublic static cryptoAlgo = 'aes-256-gcm' as const\n\tpublic static cryptoVersion = 2\n\n\tconstructor(\n\t\tprivate readonly encryptionKey?: crypto.KeyObject,\n\t) {\n\t}\n\n\tasync encrypt(value: Buffer): Promise<{ value: Buffer; version: number }> {\n\t\tif (!this.encryptionKey) {\n\t\t\tthrow new Error('encryption key not provided')\n\t\t}\n\n\t\tconst iv = crypto.randomBytes(16)\n\t\tconst cipher = crypto.createCipheriv(CryptoWrapper.cryptoAlgo, this.encryptionKey, iv)\n\t\treturn {\n\t\t\tvalue: Buffer.concat([cipher.update(value), cipher.final(), iv, cipher.getAuthTag()]),\n\t\t\tversion: CryptoWrapper.cryptoVersion,\n\t\t}\n\t}\n\n\tasync decrypt(value: Buffer, version: number): Promise<{ value: Buffer; needsReEncrypt: boolean }> {\n\t\tif (!this.encryptionKey) {\n\t\t\tthrow new Error('encryption key not provided')\n\t\t}\n\t\tif (version === 1) {\n\t\t\tconst decipher = crypto.createDecipheriv(CryptoWrapper.cryptoAlgo, this.encryptionKey, value.subarray(-16))\n\t\t\treturn {\n\t\t\t\tvalue: decipher.update(value.subarray(0, -16)),\n\t\t\t\tneedsReEncrypt: true,\n\t\t\t}\n\t\t} else if (version === 2) {\n\t\t\tconst decipher = crypto.createDecipheriv(CryptoWrapper.cryptoAlgo, this.encryptionKey, value.subarray(-32, -16))\n\t\t\tdecipher.setAuthTag(value.subarray(-16))\n\t\t\treturn {\n\t\t\t\tvalue: Buffer.concat([decipher.update(value.subarray(0, -32)), decipher.final()]),\n\t\t\t\tneedsReEncrypt: false,\n\t\t\t}\n\t\t}\n\t\tthrow new Error('unsupported version')\n\t}\n}\n"],"names":["_CryptoWrapper","crypto"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEO,MAAM,iBAAN,MAAMA,gB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