UNPKG

node-ciphers

Version:

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

36 lines (33 loc) 1.11 kB
import { createCipheriv, createDecipheriv } from 'node:crypto'; import { BaseCipher } from '../../base.mjs'; import { availableCiphers } from '../../constants.mjs'; const keyLengthToBitsMap = { 16: 128, 24: 192, 32: 256, }; class BaseAesCipher extends BaseCipher { #algorithm; #key; constructor(key, mode, encodingOptions) { super(encodingOptions); this.#key = this.dataToBuffer(key, this.encodingOptions.key); const modeBits = keyLengthToBitsMap[this.#key.byteLength]; if (!modeBits) throw new Error('Invalid key length'); this.#algorithm = `aes-${modeBits}-${mode}`; if (!availableCiphers.includes(this.#algorithm)) throw new Error('Invalid algorithm'); } get algorithm() { return this.#algorithm; } createCipher(iv, options) { return createCipheriv(this.#algorithm, this.#key, iv, options); } createDecipher(iv, options) { return createDecipheriv(this.#algorithm, this.#key, iv, options); } } export { BaseAesCipher }; //# sourceMappingURL=index.mjs.map