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.62 kB
{"version":3,"file":"index.cjs","sources":["../../../src/aes/base/index.ts"],"sourcesContent":["import {\n createCipheriv,\n createDecipheriv,\n} from 'node:crypto';\nimport type {\n BinaryLike,\n CipherCCM,\n CipherCCMOptions,\n CipherGCM,\n CipherGCMOptions,\n Cipheriv,\n DecipherCCM,\n DecipherGCM,\n Decipheriv,\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\nconst 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() {\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): Cipheriv;\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): Decipheriv;\n protected createDecipher(iv: BinaryLike | null, options?: CipherCCMOptions | CipherGCMOptions | TransformOptions) {\n return createDecipheriv(this.#algorithm, this.#key, iv, options);\n }\n}\n"],"names":["BaseCipher","availableCiphers","createCipheriv","createDecipheriv"],"mappings":";;;;;;AA0BA,MAAM,kBAAkB,GAA8C;AAClE,IAAA,EAAE,EAAE,GAAG;AACP,IAAA,EAAE,EAAE,GAAG;AACP,IAAA,EAAE,EAAE,GAAG;CACV;AAEK,MAAgB,aAEpB,SAAQA,eAA2B,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,CAAA,CAAA,EAAI,IAAI,EAAE;QAC3C,IAAI,CAACC,0BAAgB,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC;IACzF;AAEA,IAAA,IAAI,SAAS,GAAA;QACT,OAAO,IAAI,CAAC,UAAU;IAC1B;IAKU,YAAY,CAAC,EAAqB,EAAE,OAAgE,EAAA;AAC1G,QAAA,OAAOC,0BAAc,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC;IAClE;IAKU,cAAc,CAAC,EAAqB,EAAE,OAAgE,EAAA;AAC5G,QAAA,OAAOC,4BAAgB,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC;IACpE;AACH;;;;"}