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.

101 lines (98 loc) 3.66 kB
import { SecureBuffer } from '../../../secure-memory.js'; import { SecureString } from '../../../secure-string/core/secure-string-core.js'; import 'crypto'; import '../../../secure-string/advanced/entropy-analyzer.js'; import '../../../secure-string/advanced/quantum-safe.js'; import '../../../secure-string/advanced/performance-monitor.js'; import { ArrayCryptoHandler } from '../../../secure-array/crypto/ArrayCryptoHandler.js'; import { EventEmitter } from 'events'; import { FortifiedUtils } from '../../utils/utils.js'; /** * Security Manager for Fortified Function Core * Handles encryption, parameter security, and stack trace protection */ class SecurityManager extends EventEmitter { constructor(options) { super(); this.options = options; this.cryptoHandler = new ArrayCryptoHandler(`fortified_func_${Date.now()}`); } /** * Encrypt sensitive parameters for secure storage */ async encryptParameters(context, args) { const { secureParameters } = this.options; for (let i = 0; i < args.length; i++) { const shouldEncrypt = secureParameters.includes(i) || secureParameters.includes(`param${i}`); if (shouldEncrypt && args[i] != null) { try { // Convert to secure string and hash for security const secureString = new SecureString(String(args[i])); const encrypted = await secureString.hash("SHA-256", "hex"); context.encryptedParameters.set(`param${i}`, encrypted); // Store in secure buffer for memory management const buffer = SecureBuffer.from(String(args[i])); context.secureBuffers.set(`param${i}`, buffer); context.auditEntry.securityFlags.push(`param${i}_encrypted`); this.emit("parameter_encrypted", { parameter: i }); } catch (error) { this.emit("encryption_error", { parameter: i, error }); } } } } /** * Execute function with stack trace protection */ async executeWithStackProtection(originalFunction, args) { if (!this.options.stackTraceProtection) { return await originalFunction(...args); } try { return await originalFunction(...args); } catch (error) { // Sanitize stack trace to remove sensitive parameter information if (error instanceof Error && error.stack) { error.stack = FortifiedUtils.sanitizeStackTrace(error.stack); } throw error; } } /** * Generate cache key for memoization */ async generateCacheKey(args) { const serialized = JSON.stringify(args); const secureString = new SecureString(serialized); try { return (await secureString.hash("SHA-256", "hex")); } finally { secureString.destroy(); } } /** * Generate hash of parameters for audit logging */ async hashParameters(args) { const serialized = FortifiedUtils.serializeArgsForHash(args); const secureString = new SecureString(serialized); try { return (await secureString.hash("SHA-256", "hex")); } finally { secureString.destroy(); } } /** * Clean up security resources */ destroy() { this.removeAllListeners(); } } export { SecurityManager }; //# sourceMappingURL=security-manager.js.map