secure-encryption-lib
Version:
A TypeScript library for secure encryption using AES & RSA with automatic key rotation.
82 lines (81 loc) • 4.52 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _SecureEncryption_initialized, _SecureEncryption_rsaKeyManager;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecureEncryption = void 0;
const CONFIG_1 = __importDefault(require("./CONFIG"));
const RsaKeyManager_1 = require("./RsaKeyManager");
const lodash_1 = __importDefault(require("lodash"));
const crypto_1 = __importDefault(require("crypto"));
class SecureEncryption {
/**
* Creates an instance of SecureEncryption.
*
* @constructor
* @param [config]
*/
constructor(config) {
/** @private */
_SecureEncryption_initialized.set(this, false
/** @private */
);
/** @private */
_SecureEncryption_rsaKeyManager.set(this, void 0);
const thisConfig = lodash_1.default.merge(CONFIG_1.default, config);
__classPrivateFieldSet(this, _SecureEncryption_rsaKeyManager, new RsaKeyManager_1.RSAKeyManager(thisConfig), "f");
this.encryptAES = this.encryptAES.bind(this);
this.decryptAES = this.decryptAES.bind(this);
this.encryptRSA = this.encryptRSA.bind(this);
this.decryptRSA = this.decryptRSA.bind(this);
}
encryptAES(text, secretKey) {
const cipher = crypto_1.default.createCipheriv('aes-256-cbc', Buffer.from(secretKey), Buffer.alloc(16, 0));
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
decryptAES(encryptedText, secretKey) {
const decipher = crypto_1.default.createDecipheriv('aes-256-cbc', Buffer.from(secretKey), Buffer.alloc(16, 0));
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
encryptRSA(text) {
return __awaiter(this, void 0, void 0, function* () {
const { publicKey } = yield __classPrivateFieldGet(this, _SecureEncryption_rsaKeyManager, "f").getKeys();
return crypto_1.default.publicEncrypt(publicKey, Buffer.from(text)).toString('base64');
});
}
decryptRSA(encryptedText) {
return __awaiter(this, void 0, void 0, function* () {
const { privateKey } = yield __classPrivateFieldGet(this, _SecureEncryption_rsaKeyManager, "f").getKeys();
return crypto_1.default
.privateDecrypt(privateKey, Buffer.from(encryptedText, 'base64'))
.toString('utf8');
});
}
}
exports.SecureEncryption = SecureEncryption;
_SecureEncryption_initialized = new WeakMap(), _SecureEncryption_rsaKeyManager = new WeakMap();