node-ciphers
Version:
Lightweight AES and DES encryption library for Node.js, featuring flexible encoding, multiple cipher modes, and TypeScript support.
46 lines (42 loc) • 1.68 kB
JavaScript
'use strict';
const node_crypto = require('node:crypto');
const index = require('./index.cjs');
class BaseAesEncryptAndDecrypt extends index.BaseAesCipher {
decrypt(encryptedData, iv, encodingOptions, decipherOptions) {
try {
const decipher = this.createDecipher(this.dataToBuffer(iv, encodingOptions?.iv || this.encodingOptions.iv), decipherOptions);
return this.createOkResult(this.getDecipherResult(decipher, encryptedData, encodingOptions));
}
catch (error) {
return this.createErrorResult(error);
}
}
decryptToJson(encryptedData, iv, encodingOptions, decipherOptions) {
const result = this.decrypt(encryptedData, iv, encodingOptions, decipherOptions);
if (!result.ok)
return result;
return this.parseJson(result.value);
}
encrypt(data, encodingOptions, cipherOptions) {
const iv = node_crypto.randomBytes(16);
try {
return this.createOkResult({
data: this.getCipherResult(this.createCipher(iv, cipherOptions), data, encodingOptions),
iv: iv.toString(encodingOptions?.iv || this.encodingOptions.iv),
});
}
catch (error) {
return this.createErrorResult(error);
}
}
encryptJson(data, encodingOptions, cipherOptions) {
try {
return this.encrypt(JSON.stringify(data), encodingOptions, cipherOptions);
}
catch (error) {
return this.createErrorResult(error);
}
}
}
exports.BaseAesEncryptAndDecrypt = BaseAesEncryptAndDecrypt;
//# sourceMappingURL=encrypt-and-decrypt.cjs.map