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).

88 lines (87 loc) 3.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createZipFromPaths = createZipFromPaths; exports.extractZipToDir = extractZipToDir; exports.readBinaryFile = readBinaryFile; exports.writeBinaryFile = writeBinaryFile; exports.detectMimeType = detectMimeType; exports.hashFile = hashFile; exports.collectMetadata = collectMetadata; exports.saveEncryptedJson = saveEncryptedJson; exports.loadEncryptedJson = loadEncryptedJson; // src/core.ts const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const mime_types_1 = __importDefault(require("mime-types")); const crypto_1 = __importDefault(require("crypto")); const adm_zip_1 = __importDefault(require("adm-zip")); // Crear un ZIP a partir de múltiples rutas (archivos o carpetas) function createZipFromPaths(paths, outputZipPath) { const zip = new adm_zip_1.default(); for (const p of paths) { if (!fs_1.default.existsSync(p)) throw new Error(`Path no existe: ${p}`); const stats = fs_1.default.statSync(p); if (stats.isDirectory()) { zip.addLocalFolder(p, path_1.default.basename(p)); } else { zip.addLocalFile(p); } } zip.writeZip(outputZipPath); return outputZipPath; } // Extraer un ZIP a un directorio function extractZipToDir(zipPath, outputDir) { if (!fs_1.default.existsSync(zipPath)) throw new Error(`ZIP no encontrado: ${zipPath}`); const zip = new adm_zip_1.default(zipPath); zip.extractAllTo(outputDir, true); } // Leer un archivo binario function readBinaryFile(filePath) { if (!fs_1.default.existsSync(filePath)) throw new Error(`Archivo no encontrado: ${filePath}`); return fs_1.default.readFileSync(filePath); } // Escribir un archivo binario function writeBinaryFile(filePath, data) { fs_1.default.writeFileSync(filePath, data); } // Detectar MIME type function detectMimeType(filePath) { return mime_types_1.default.lookup(filePath) || 'application/octet-stream'; } // Calcular hash SHA256 function hashFile(filePath) { const hash = crypto_1.default.createHash('sha256'); const data = fs_1.default.readFileSync(filePath); hash.update(data); return hash.digest('hex'); } // Recopilar metadatos de un archivo function collectMetadata(filePath) { if (!fs_1.default.existsSync(filePath)) throw new Error(`Archivo no encontrado: ${filePath}`); return { filename: path_1.default.basename(filePath), mime: detectMimeType(filePath), size: fs_1.default.statSync(filePath).size, sha256: hashFile(filePath) }; } // Guardar objeto cifrado como JSON function saveEncryptedJson(outputPath, data) { fs_1.default.writeFileSync(outputPath, JSON.stringify(data, null, 2)); } // Leer objeto cifrado desde archivo JSON function loadEncryptedJson(jsonPath) { if (!fs_1.default.existsSync(jsonPath)) throw new Error(`Archivo no encontrado: ${jsonPath}`); const raw = fs_1.default.readFileSync(jsonPath, 'utf-8'); return JSON.parse(raw); }