node-ciphers
Version:
Lightweight AES and DES encryption library for Node.js, featuring flexible encoding, multiple cipher modes, and TypeScript support.
35 lines (33 loc) • 1.08 kB
JavaScript
import { availableCiphers } from "../../../_internals/constants.js";
import { BaseCipher } from "../../../_internals/base-cipher.js";
import { createCipheriv, createDecipheriv } from "node:crypto";
//#region src/aes/_internals/base/index.ts
const keyLengthToBitsMap = {
16: 128,
24: 192,
32: 256
};
var BaseAesCipher = class 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);
}
};
//#endregion
export { BaseAesCipher };
//# sourceMappingURL=index.js.map