UNPKG

fortify2-js

Version:

MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.

112 lines (109 loc) 3.8 kB
import { promisify } from 'util'; import { existsSync, mkdirSync } from 'fs'; import path__default from 'path'; import zlib from 'zlib'; import '../../types.js'; import '../../core/hash/hash-core.js'; import '../../core/hash/hash-types.js'; import * as crypto from 'crypto'; import '../../core/hash/hash-security.js'; import '../../core/hash/hash-advanced.js'; import '../../algorithms/hash-algorithms.js'; import '../../core/keys/keys-types.js'; import '../../core/keys/keys-logger.js'; import '../../core/keys/keys-utils.js'; import '../../core/keys/algorithms/mods/PBKDF2Algo.js'; import { SecureRandom } from '../../core/random/random-core.js'; import '../../core/random/random-types.js'; import '../../core/random/random-sources.js'; import 'nehonix-uri-processor'; import '../../utils/memory/index.js'; import 'argon2'; import 'child_process'; import '../../types/secure-memory.js'; import 'https'; import '../runtime-verification.js'; import '../tamper-evident-logging.js'; import '../secure-string/advanced/entropy-analyzer.js'; import '../secure-string/advanced/quantum-safe.js'; import '../secure-string/advanced/performance-monitor.js'; import 'nehoid'; import '../../core/password/index.js'; /** * Ensure directory exists for file cache operations */ const ensureDirectoryExists = async (filePath) => { const dir = path__default.dirname(filePath); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); } }; /** * Encrypt data for file storage */ const encryptFileData = (data, key) => { const encryptionKey = key || SecureRandom.getRandomBytes(32); const iv = SecureRandom.getRandomBytes(16); const cipher = crypto.createCipheriv("aes-256-gcm", encryptionKey, iv); let encrypted = cipher.update(data, "utf8", "base64"); encrypted += cipher.final("base64"); const authTag = cipher.getAuthTag(); return { encrypted, iv: iv.toString("base64"), authTag: authTag.toString("base64"), key: encryptionKey.toString("base64"), }; }; /** * Decrypt data from file storage */ const decryptFileData = (encrypted, iv, authTag, key) => { const keyBuffer = Buffer.from(key, "base64"); const ivBuffer = Buffer.from(iv, "base64"); const authTagBuffer = Buffer.from(authTag, "base64"); const decipher = crypto.createDecipheriv("aes-256-gcm", keyBuffer, ivBuffer); decipher.setAuthTag(authTagBuffer); let decrypted = decipher.update(encrypted, "base64", "utf8"); decrypted += decipher.final("utf8"); return decrypted; }; /** * Compress data if beneficial */ const compressFileData = async (data) => { if (data.length < 1024) { // Don't compress small data return { data, compressed: false }; } try { const deflate = promisify(zlib.deflate); const compressed = await deflate(Buffer.from(data, "utf8")); const compressedString = compressed.toString("base64"); if (compressedString.length < data.length * 0.9) { return { data: compressedString, compressed: true }; } } catch (error) { console.warn("File compression failed:", error); } return { data, compressed: false }; }; /** * Decompress data */ const decompressFileData = async (data, compressed) => { if (!compressed) return data; try { const inflate = promisify(zlib.inflate); const decompressed = await inflate(Buffer.from(data, "base64")); return decompressed.toString("utf8"); } catch (error) { console.error("File decompression failed:", error); throw new Error("Data decompression failed"); } }; export { compressFileData, decompressFileData, decryptFileData, encryptFileData, ensureDirectoryExists }; //# sourceMappingURL=cacheSys.utils.js.map