system-secured-storage
Version:
A Node.js project that allows users to store encrypted key-value data locally on their system. This project serves as an alternate storage solution to SQLite but with enhanced security features, leveraging AES encryption to ensure the confidentiality and
46 lines • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptionService = void 0;
const node_crypto_1 = require("node:crypto");
/**
* The service use 'aes-256-ctr' aes algorithm by default.
*
* @exports
* @class EncryptionService
*/
class EncryptionService {
/**
* Create an instance of the encrytion service.
*
* @param {string} key - The AES encryption key.
* @param {string} iv - The iv key.
*/
constructor(key, iv) {
this.algorithm = 'aes-256-ctr';
this.key = Buffer.from(key, 'hex');
this.iv = Buffer.from(iv, 'hex');
}
/**
* Encrypt data.
*
* @param {data} data - The data to be encrypted.
* @returns {string} - Encrypted data.
*/
encrypt(data) {
const stringifiedData = JSON.stringify(data);
const cipher = (0, node_crypto_1.createCipheriv)(this.algorithm, this.key, this.iv);
return cipher.update(stringifiedData, 'utf8', 'hex') + cipher.final('hex');
}
/**
* Decrypt data.
*
* @param {string} encryptedData - The encrypted data.
* @returns {T} - The decrypted data.
*/
decrypt(encryptedData) {
const decipher = (0, node_crypto_1.createDecipheriv)(this.algorithm, this.key, this.iv);
return JSON.parse(decipher.update(encryptedData, 'hex', 'utf8') + decipher.final('utf8'));
}
}
exports.EncryptionService = EncryptionService;
//# sourceMappingURL=encryption.service.js.map