UNPKG

secure-encryption-lib

Version:

A TypeScript library for secure encryption using AES & RSA with automatic key rotation.

91 lines (90 loc) 4.86 kB
"use strict"; 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 _RSAKeyManager_initialized, _RSAKeyManager_redis; Object.defineProperty(exports, "__esModule", { value: true }); exports.RSAKeyManager = void 0; const CONFIG_1 = __importDefault(require("./CONFIG")); const Redis_1 = __importDefault(require("./Redis")); const lodash_1 = __importDefault(require("lodash")); const crypto_1 = __importDefault(require("crypto")); class RSAKeyManager { /** * Creates an instance of RSAKeyManager. * * @constructor * @param [config] */ constructor(config) { /** @private */ _RSAKeyManager_initialized.set(this, false /** @private */ ); /** @private */ _RSAKeyManager_redis.set(this, void 0); const thisConfig = lodash_1.default.merge(CONFIG_1.default, config); const { REDIS_CONFIG, SECURE_ENCRYPTION_CONFIG } = thisConfig; __classPrivateFieldSet(this, _RSAKeyManager_redis, new Redis_1.default(REDIS_CONFIG), "f"); this.REDIS_CONFIG = __classPrivateFieldGet(this, _RSAKeyManager_redis, "f").REDIS_CONFIG; this.SECURE_ENCRYPTION_CONFIG = SECURE_ENCRYPTION_CONFIG; this.initialize = this.initialize.bind(this); this.generateKeys = this.generateKeys.bind(this); this.getKeys = this.getKeys.bind(this); } /** * Initialize the RSAKeyManager instance. It internally creates a Redis connection * * @async * @returns */ initialize() { return __awaiter(this, void 0, void 0, function* () { yield __classPrivateFieldGet(this, _RSAKeyManager_redis, "f").initialize(); __classPrivateFieldSet(this, _RSAKeyManager_initialized, true, "f"); }); } generateKeys() { return __awaiter(this, void 0, void 0, function* () { const { publicKey, privateKey } = crypto_1.default.generateKeyPairSync('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'pkcs1', format: 'pem' }, privateKeyEncoding: { type: 'pkcs1', format: 'pem' } }); yield __classPrivateFieldGet(this, _RSAKeyManager_redis, "f").setKey('rsa_public_key', publicKey); yield __classPrivateFieldGet(this, _RSAKeyManager_redis, "f").setKey('rsa_private_key', privateKey); }); } getKeys() { return __awaiter(this, void 0, void 0, function* () { const publicKey = yield __classPrivateFieldGet(this, _RSAKeyManager_redis, "f").getKey('rsa_public_key'); const privateKey = yield __classPrivateFieldGet(this, _RSAKeyManager_redis, "f").getKey('rsa_private_key'); if (!publicKey || !privateKey) { throw new Error('RSA keys not found in Redis. Generate new keys.'); } return { publicKey, privateKey }; }); } } exports.RSAKeyManager = RSAKeyManager; _RSAKeyManager_initialized = new WeakMap(), _RSAKeyManager_redis = new WeakMap();