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.15 kB
JavaScript
import { createCipheriv, createDecipheriv } from 'node:crypto';
import { BaseCipher } from '../../base.mjs';
import { availableCiphers } from '../../constants.mjs';
const keyLengthToModePrefixMap = {
8: '',
16: '-ede',
24: '-ede3',
};
class BaseDesCipher extends BaseCipher {
#algorithm;
#key;
constructor(key, mode, encodingOptions) {
super(encodingOptions);
this.#key = this.dataToBuffer(key, this.encodingOptions.key);
const desModePrefix = keyLengthToModePrefixMap[this.#key.byteLength];
if (desModePrefix === undefined)
throw new Error('Invalid key length');
this.#algorithm = `des${desModePrefix}-${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 { BaseDesCipher };
//# sourceMappingURL=index.mjs.map