{"version":3,"file":"decrypt.mjs","sources":["../../../src/utils/encryption/decrypt.ts"],"sourcesContent":["import { Decipheriv, scryptSync, CipherKey, BinaryLike, createDecipheriv } from 'crypto';\nimport { EncryptionStrategy, Strategies, Algorithm } from '../../../types';\n\n// different key values depending on algorithm chosen\nconst getDecryptionStrategy = (algorithm: Algorithm): EncryptionStrategy => {\n  const strategies: Strategies = {\n    'aes-128-ecb'(key: string): Decipheriv {\n      const hashedKey = scryptSync(key, '', 16);\n      const initVector: BinaryLike | null = null;\n      const securityKey: CipherKey = hashedKey;\n      return createDecipheriv(algorithm, securityKey, initVector);\n    },\n    aes128(key: string): Decipheriv {\n      const hashedKey = scryptSync(key, '', 32);\n      const initVector: BinaryLike | null = hashedKey.subarray(16);\n      const securityKey: CipherKey = hashedKey.subarray(0, 16);\n      return createDecipheriv(algorithm, securityKey, initVector);\n    },\n    aes192(key: string): Decipheriv {\n      const hashedKey = scryptSync(key, '', 40);\n      const initVector: BinaryLike | null = hashedKey.subarray(24);\n      const securityKey: CipherKey = hashedKey.subarray(0, 24);\n      return createDecipheriv(algorithm, securityKey, initVector);\n    },\n    aes256(key: string): Decipheriv {\n      const hashedKey = scryptSync(key, '', 48);\n      const initVector: BinaryLike | null = hashedKey.subarray(32);\n      const securityKey: CipherKey = hashedKey.subarray(0, 32);\n      return createDecipheriv(algorithm, securityKey, initVector);\n    },\n  };\n\n  return strategies[algorithm];\n};\n\n/**\n * It creates a cipher instance used for decryption\n *\n * @param key - The decryption key\n * @param algorithm - The algorithm to use to create the Cipher\n *\n * @returns A {@link Decipheriv} instance created with the given key & algorithm\n */\nexport const createDecryptionCipher = (\n  key: string,\n  algorithm: Algorithm = 'aes-128-ecb'\n): Decipheriv => {\n  return getDecryptionStrategy(algorithm)(key);\n};\n"],"names":["getDecryptionStrategy","algorithm","strategies","key","hashedKey","scryptSync","initVector","securityKey","createDecipheriv","aes128","subarray","aes192","aes256","createDecryptionCipher"],"mappings":";;AAGA;AACA,MAAMA,wBAAw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