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.

206 lines (202 loc) 6.19 kB
'use strict'; var secureMemory = require('../../secure-memory.js'); var index = require('../types/index.js'); /** * Buffer Manager Module * Handles secure buffer operations for SecureString */ /** * Manages secure buffer operations for SecureString */ class BufferManager { constructor(initialValue = "", options = {}) { this._isDestroyed = false; this.options = { ...index.DEFAULT_SECURE_STRING_OPTIONS, ...options }; this.buffer = this.createBuffer(initialValue); } /** * Creates a new secure buffer with the specified value */ createBuffer(value) { return secureMemory.SecureBuffer.from(value, { protectionLevel: this.options.protectionLevel, enableEncryption: this.options.enableEncryption, enableFragmentation: this.options.enableFragmentation, enableCanaries: this.options.enableCanaries, enableObfuscation: this.options.enableObfuscation, autoLock: this.options.autoLock, quantumSafe: this.options.quantumSafe, }); } /** * Gets the current buffer (read-only access) */ getBuffer() { this.ensureNotDestroyed(); return this.buffer.getBuffer(); } /** * Gets the string value from the buffer */ getString() { this.ensureNotDestroyed(); const buffer = this.buffer.getBuffer(); return new TextDecoder(this.options.encoding).decode(buffer); } /** * Updates the buffer with a new string value */ updateBuffer(newValue) { this.ensureNotDestroyed(); // Destroy the old buffer this.buffer.destroy(); // Create a new buffer with the new value this.buffer = this.createBuffer(newValue); } /** * Gets the byte length of the buffer */ getByteLength() { this.ensureNotDestroyed(); return this.buffer.getBuffer().length; } /** * Gets the character length of the string */ getCharacterLength() { this.ensureNotDestroyed(); return this.getString().length; } /** * Checks if the buffer is empty */ isEmpty() { this.ensureNotDestroyed(); return this.getCharacterLength() === 0; } /** * Creates a copy of the current buffer */ clone() { this.ensureNotDestroyed(); const currentValue = this.getString(); return new BufferManager(currentValue, this.options); } /** * Converts the buffer to a Uint8Array (copy) */ toUint8Array() { this.ensureNotDestroyed(); const buffer = this.buffer.getBuffer(); return new Uint8Array(buffer); } /** * Creates a buffer from a Uint8Array */ static fromUint8Array(data, options = {}, encoding = "utf-8") { const decoder = new TextDecoder(encoding); const value = decoder.decode(data); return new BufferManager(value, { ...options, encoding }); } /** * Gets memory usage information */ getMemoryUsage() { this.ensureNotDestroyed(); const bufferSize = this.getByteLength(); const actualLength = this.getCharacterLength(); return { bufferSize, actualLength, overhead: bufferSize - actualLength, isFragmented: this.options.enableFragmentation, isEncrypted: this.options.enableEncryption, }; } /** * Gets the current options */ getOptions() { return { ...this.options }; } /** * Updates the options (creates new buffer if needed) */ updateOptions(newOptions) { this.ensureNotDestroyed(); const oldOptions = this.options; this.options = { ...this.options, ...newOptions }; // Check if we need to recreate the buffer const needsRecreation = oldOptions.protectionLevel !== this.options.protectionLevel || oldOptions.enableEncryption !== this.options.enableEncryption || oldOptions.enableFragmentation !== this.options.enableFragmentation || oldOptions.enableCanaries !== this.options.enableCanaries || oldOptions.enableObfuscation !== this.options.enableObfuscation || oldOptions.autoLock !== this.options.autoLock || oldOptions.quantumSafe !== this.options.quantumSafe; if (needsRecreation) { const currentValue = this.getString(); this.buffer.destroy(); this.buffer = this.createBuffer(currentValue); } } /** * Ensures the buffer manager hasn't been destroyed */ ensureNotDestroyed() { if (this._isDestroyed) { throw new Error("BufferManager has been destroyed and cannot be used"); } } /** * Checks if the buffer manager is destroyed */ isDestroyed() { return this._isDestroyed; } /** * Destroys the buffer and clears all data */ destroy() { if (!this._isDestroyed) { this.buffer.destroy(); this._isDestroyed = true; } } /** * Securely wipes the buffer content */ wipe() { this.ensureNotDestroyed(); this.updateBuffer(""); } /** * Gets debug information about the buffer */ getDebugInfo() { if (this._isDestroyed) { return { isDestroyed: true, options: this.options, memoryUsage: { bufferSize: 0, actualLength: 0, overhead: 0, isFragmented: false, isEncrypted: false, }, characterLength: 0, byteLength: 0, }; } return { isDestroyed: false, options: this.options, memoryUsage: this.getMemoryUsage(), characterLength: this.getCharacterLength(), byteLength: this.getByteLength(), }; } } exports.BufferManager = BufferManager; //# sourceMappingURL=buffer-manager.js.map