UNPKG

cross-crypto-ts

Version:

Cifrado híbrido seguro con interoperabilidad entre lenguajes como Python, TypeScript y Rust, basado en AES-GCM (256 bits) y RSA-OAEP (4096 bits).

45 lines (44 loc) 2.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.encryptFileHybrid = encryptFileHybrid; exports.decryptFileHybrid = decryptFileHybrid; // src/file_crypto.ts const fs_1 = __importDefault(require("fs")); const encrypt_1 = require("./encrypt"); const core_1 = require("./core"); const decrypt_1 = require("./decrypt"); function encryptFileHybrid(paths, publicKey, options = {}) { if (!paths.length || !paths.every(fs_1.default.existsSync)) { throw new Error('Una o más rutas no existen o la lista está vacía.'); } const zipName = options.zipOutput || `temp_${Date.now()}.zip`; const zipPath = (0, core_1.createZipFromPaths)(paths, zipName); const binaryData = (0, core_1.readBinaryFile)(zipPath); const encrypted = (0, encrypt_1.encryptHybrid)(binaryData, publicKey, 'binary'); if (options.attachMetadata) { encrypted.meta = (0, core_1.collectMetadata)(zipPath); } if (options.saveFile) { const outPath = options.outputEnc || zipPath + '.enc'; (0, core_1.saveEncryptedJson)(outPath, encrypted); } fs_1.default.unlinkSync(zipPath); // Eliminar el ZIP temporal return encrypted; } function decryptFileHybrid(encPath, privateKey, extractTo, cleanupZip = true) { if (!fs_1.default.existsSync(encPath)) { throw new Error(`Archivo cifrado no encontrado: ${encPath}`); } const encryptedObj = (0, core_1.loadEncryptedJson)(encPath); const decryptedBinary = (0, decrypt_1.decryptHybrid)(encryptedObj, privateKey); const tempZipPath = encPath.replace(/\.enc$/, '.zip'); (0, core_1.writeBinaryFile)(tempZipPath, Buffer.from(decryptedBinary)); const outputDir = extractTo || encPath.replace(/\.enc$/, '_output'); (0, core_1.extractZipToDir)(tempZipPath, outputDir); if (cleanupZip) fs_1.default.unlinkSync(tempZipPath); return outputDir; }