UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

129 lines 4.84 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.AccountCrypto = void 0; const crypto = __importStar(require("crypto")); const ENCRYPTION_CONFIG = { algorithm: 'aes-256-cbc', keyLength: 32, ivLength: 16, defaultMasterKey: 'polyv-cli-default-master-key-2024' }; class AccountCrypto { constructor(masterKey) { const keySource = masterKey || process.env['POLYV_MASTER_KEY'] || ENCRYPTION_CONFIG.defaultMasterKey; this.masterKey = this.deriveKey(keySource); } deriveKey(masterKey) { const salt = Buffer.from('polyv-cli-salt-2024', 'utf8'); return crypto.pbkdf2Sync(masterKey, salt, 100000, ENCRYPTION_CONFIG.keyLength, 'sha256'); } encrypt(plaintext) { try { const iv = crypto.randomBytes(ENCRYPTION_CONFIG.ivLength); const cipher = crypto.createCipheriv(ENCRYPTION_CONFIG.algorithm, this.masterKey, iv); let encrypted = cipher.update(plaintext, 'utf8', 'hex'); encrypted += cipher.final('hex'); const metadata = { algorithm: ENCRYPTION_CONFIG.algorithm, iv: iv.toString('hex'), authTag: '' }; return { encrypted, metadata }; } catch (error) { throw new Error(`Failed to encrypt account secret: ${error instanceof Error ? error.message : 'Unknown error'}`); } } decrypt(encrypted, metadata) { try { if (metadata.algorithm !== ENCRYPTION_CONFIG.algorithm) { throw new Error(`Unsupported encryption algorithm: ${metadata.algorithm}`); } const iv = Buffer.from(metadata.iv, 'hex'); const decipher = crypto.createDecipheriv(metadata.algorithm, this.masterKey, iv); let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); return decrypted; } catch (error) { throw new Error(`Failed to decrypt account secret: ${error instanceof Error ? error.message : 'Unknown error'}`); } } encryptSimple(plaintext) { const { encrypted, metadata } = this.encrypt(plaintext); const combined = { data: encrypted, meta: metadata }; return Buffer.from(JSON.stringify(combined)).toString('base64'); } decryptSimple(encryptedData) { try { const combined = JSON.parse(Buffer.from(encryptedData, 'base64').toString('utf8')); return this.decrypt(combined.data, combined.meta); } catch (error) { throw new Error(`Failed to decrypt account secret: Invalid encrypted data format`); } } validateEncryptedData(encryptedData) { try { this.decryptSimple(encryptedData); return true; } catch { return false; } } static generateMasterKey() { return crypto.randomBytes(32).toString('hex'); } testEncryption(testData = 'test-secret-data') { try { const encrypted = this.encryptSimple(testData); const decrypted = this.decryptSimple(encrypted); return decrypted === testData; } catch { return false; } } } exports.AccountCrypto = AccountCrypto; //# sourceMappingURL=account-crypto.js.map