UNPKG

node-ciphers

Version:

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

1 lines 2.7 kB
{"version":3,"file":"index.mjs","sources":["../../../src/des/base/index.ts"],"sourcesContent":["import {\n createCipheriv,\n createDecipheriv,\n} from 'node:crypto';\nimport type { BinaryLike } from 'node:crypto';\nimport type { TransformOptions } from 'node:stream';\n\nimport { BaseCipher } from '../../base';\nimport { availableCiphers } from '../../constants';\nimport type {\n DesCipherAlgorithm,\n DesCipherEncodingOptions,\n DesCipherMode,\n} from '../../types';\n\nconst keyLengthToModePrefixMap: Readonly<Record<number, '' | '-ede3' | '-ede'>> = {\n 8: '',\n 16: '-ede',\n 24: '-ede3',\n};\n\nexport abstract class BaseDesCipher extends BaseCipher {\n readonly #algorithm: DesCipherAlgorithm;\n readonly #key: NodeJS.ArrayBufferView;\n\n constructor(key: BinaryLike, mode: DesCipherMode, encodingOptions?: DesCipherEncodingOptions) {\n super(encodingOptions);\n this.#key = this.dataToBuffer(key, this.encodingOptions.key);\n const desModePrefix = keyLengthToModePrefixMap[this.#key.byteLength];\n if (desModePrefix === undefined) throw new Error('Invalid key length');\n this.#algorithm = `des${desModePrefix}-${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 | null, options?: TransformOptions) {\n return createCipheriv(this.#algorithm, this.#key, iv, options);\n }\n\n protected createDecipher(iv: BinaryLike | null, options?: TransformOptions) {\n return createDecipheriv(this.#algorithm, this.#key, iv, options);\n }\n}\n"],"names":[],"mappings":";;;;AAeA,MAAM,wBAAwB,GAAoD;AAC9E,IAAA,CAAC,EAAE,EAAE;AACL,IAAA,EAAE,EAAE,MAAM;AACV,IAAA,EAAE,EAAE,OAAO;CACd;AAEK,MAAgB,aAAc,SAAQ,UAAU,CAAA;AACzC,IAAA,UAAU;AACV,IAAA,IAAI;AAEb,IAAA,WAAA,CAAY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