node-ciphers
Version:
Lightweight AES and DES encryption library for Node.js, featuring flexible encoding, multiple cipher modes, and TypeScript support.
38 lines (34 loc) • 1.14 kB
JavaScript
;
const node_crypto = require('node:crypto');
const base = require('../../base.cjs');
const constants = require('../../constants.cjs');
const keyLengthToBitsMap = {
16: 128,
24: 192,
32: 256,
};
class BaseAesCipher extends base.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 (!constants.availableCiphers.includes(this.#algorithm))
throw new Error('Invalid algorithm');
}
get algorithm() {
return this.#algorithm;
}
createCipher(iv, options) {
return node_crypto.createCipheriv(this.#algorithm, this.#key, iv, options);
}
createDecipher(iv, options) {
return node_crypto.createDecipheriv(this.#algorithm, this.#key, iv, options);
}
}
exports.BaseAesCipher = BaseAesCipher;
//# sourceMappingURL=index.cjs.map