UNPKG

node-ciphers

Version:

Lightweight AES and DES encryption library for Node.js, featuring flexible encoding, multiple cipher modes, and TypeScript support.

1 lines 3.55 kB
{"version":3,"file":"index.mjs","sources":["../../../src/aes/base/index.ts"],"sourcesContent":["import {\n createCipheriv,\n createDecipheriv,\n} from 'node:crypto';\nimport type {\n BinaryLike,\n Cipher,\n CipherCCM,\n CipherCCMOptions,\n CipherGCM,\n CipherGCMOptions,\n Decipher,\n DecipherCCM,\n DecipherGCM,\n} from 'node:crypto';\nimport type { TransformOptions } from 'node:stream';\n\nimport { BaseCipher } from '../../base';\nimport { availableCiphers } from '../../constants';\nimport type {\n AesCipherAlgorithm,\n AesCipherEncodingOptions,\n AesCipherMode,\n HasAuthTagAesCipherEncodingOptions,\n} from '../../types';\n\nexport const keyLengthToBitsMap: Readonly<Record<number, 128 | 192 | 256>> = {\n 16: 128,\n 24: 192,\n 32: 256,\n};\n\nexport abstract class BaseAesCipher<\n EncodingOptions extends HasAuthTagAesCipherEncodingOptions = AesCipherEncodingOptions,\n> extends BaseCipher<EncodingOptions> {\n readonly #algorithm: AesCipherAlgorithm;\n readonly #key: NodeJS.ArrayBufferView;\n\n constructor(key: BinaryLike, mode: AesCipherMode, encodingOptions?: EncodingOptions) {\n super(encodingOptions);\n this.#key = this.dataToBuffer(key, this.encodingOptions.key);\n const modeBits = keyLengthToBitsMap[this.#key.byteLength];\n if (!modeBits) throw new Error('Invalid key length');\n this.#algorithm = `aes-${modeBits}-${mode}`;\n if (!availableCiphers.includes(this.#algorithm)) throw new Error('Invalid algorithm');\n }\n\n get algorithm(): AesCipherAlgorithm {\n return this.#algorithm;\n }\n\n protected createCipher(iv: BinaryLike, options: CipherCCMOptions): CipherCCM;\n protected createCipher(iv: BinaryLike, options: CipherGCMOptions): CipherGCM;\n protected createCipher(iv: BinaryLike | null, options?: TransformOptions): Cipher;\n protected createCipher(iv: BinaryLike | null, options?: CipherCCMOptions | CipherGCMOptions | TransformOptions) {\n return createCipheriv(this.#algorithm, this.#key, iv, options);\n }\n\n protected createDecipher(iv: BinaryLike, options: CipherCCMOptions): DecipherCCM;\n protected createDecipher(iv: BinaryLike, options: CipherGCMOptions): DecipherGCM;\n protected createDecipher(iv: BinaryLike | null, options?: TransformOptions): Decipher;\n protected createDecipher(iv: BinaryLike | null, options?: CipherCCMOptions | CipherGCMOptions | TransformOptions) {\n return createDecipheriv(this.#algorithm, this.#key, iv, options);\n }\n}\n"],"names":[],"mappings":";;;;AA0Ba,MAAA,kBAAkB,GAA8C;AACzE,IAAA,EAAE,EAAE,GAAG;AACP,IAAA,EAAE,EAAE,GAAG;AACP,IAAA,EAAE,EAAE,GAAG;;AAGL,MAAgB,aAEpB,SAAQ,UAA2B,CAAA;AACxB,IAAA,UAAU;AACV,IAAA,IAAI;AAEb,IAAA,WAAA,CAAY,GAAe,EAAE,IAAmB,EAAE,eAAiC,EAAA;QAC/E,KAAK,CAAC,eAAe,CAAC;AACtB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC;QAC5D,MAAM,QAAQ,GAAG,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC;AACzD,QAAA,IAAI,CAAC,QAAQ;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC;QACpD,IAAI,CAAC,UAAU,GAAG,CAAA,IAAA,EAAO,QAAQ,CAAI,CAAA,EAAA,IAAI,EAAE;QAC3C,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC;;AAGzF,IAAA,IAAI,SAAS,GAAA;QACT,OAAO,IAAI,CAAC,UAAU;;IAMhB,YAAY,CAAC,EAAqB,EAAE,OAAgE,EAAA;AAC1G,QAAA,OAAO,cAAc,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC;;IAMxD,cAAc,CAAC,EAAqB,EAAE,OAAgE,EAAA;AAC5G,QAAA,OAAO,gBAAgB,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC;;AAEvE;;;;"}