UNPKG

streamby-core

Version:

StreamBy middleware framework for media storage management

76 lines (75 loc) 3.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.isEncryptionKeySet = exports.decrypt = exports.encrypt = exports.setEncryptionKey = void 0; const crypto = __importStar(require("crypto")); const ALGORITHM = 'aes-256-cbc'; const IV_LENGTH = 16; // For AES, this is always 16 let ENCRYPTION_KEY = undefined; const setEncryptionKey = (key) => { if (key.length !== 64) { // 32 bytes * 2 hex characters per byte throw new Error('Invalid encryption key length. Key must be 32 bytes (64 hexadecimal characters).'); } ENCRYPTION_KEY = key; }; exports.setEncryptionKey = setEncryptionKey; const encrypt = (text) => { if (!ENCRYPTION_KEY) { throw new Error('Encryption key is not set. Cannot encrypt credentials.'); } const iv = crypto.randomBytes(IV_LENGTH); const cipher = crypto.createCipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, 'hex'), iv); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return iv.toString('hex') + ':' + encrypted.toString('hex'); }; exports.encrypt = encrypt; const decrypt = (text) => { if (!ENCRYPTION_KEY) { throw new Error('Encryption key is not set. Cannot decrypt credentials.'); } const textParts = text.split(':'); const iv = Buffer.from(textParts.shift(), 'hex'); const encryptedText = Buffer.from(textParts.join(':'), 'hex'); const decipher = crypto.createDecipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY, 'hex'), iv); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); }; exports.decrypt = decrypt; const isEncryptionKeySet = () => { return !!ENCRYPTION_KEY; }; exports.isEncryptionKeySet = isEncryptionKeySet;