UNPKG

@webbuf/fixedbuf

Version:

Fixed-sized buffers optimized with Rust/WASM for the web, node.js, deno, and bun.

92 lines (91 loc) 2.94 kB
import { WebBuf } from "@webbuf/webbuf"; export class FixedBuf { _buf; _size; constructor(size, buf) { if (buf.length !== size) { throw new Error("invalid size error"); } this._buf = buf; this._size = size; } get buf() { return this._buf; } static fromBuf(size, buf) { return new FixedBuf(size, buf); } static alloc(size, fill) { const buf = WebBuf.alloc(size, fill); return FixedBuf.fromBuf(size, buf); } static fromHex(size, hex) { const buf = WebBuf.from(hex, "hex"); return FixedBuf.fromBuf(size, buf); } toHex() { return this._buf.toString("hex"); } static fromBase64(size, base64) { try { const buf = WebBuf.from(base64, "base64"); return FixedBuf.fromBuf(size, buf); } catch { throw new Error("invalid encoding"); } } toBase64() { return this._buf.toString("base64"); } /** * Encode this buffer as a base32 string. * * @param options - Options for encoding * @param options.alphabet - The alphabet to use (default: "Crockford") * @param options.padding - Whether to use padding for Rfc4648* alphabets (default: true) * @returns The base32 encoded string */ toBase32(options) { return this._buf.toBase32(options); } /** * Create a FixedBuf from a base32 encoded string. * * @param size - The expected size of the decoded buffer * @param str - The base32 encoded string * @param options - Options for decoding * @param options.alphabet - The alphabet to use (default: "Crockford") * @param options.padding - Whether the string uses padding for Rfc4648* alphabets (default: true) * @returns The decoded FixedBuf */ static fromBase32(size, str, options) { const buf = WebBuf.fromBase32(str, options); return FixedBuf.fromBuf(size, buf); } static fromRandom(size) { const buf = crypto.getRandomValues(WebBuf.alloc(size)); return FixedBuf.fromBuf(size, buf); } clone() { return FixedBuf.fromBuf(this._size, new WebBuf(this._buf)); } toReverse() { const cloneedReverse = this._buf.toReverse(); return FixedBuf.fromBuf(this._size, cloneedReverse); } /** * Securely wipe the buffer by filling it with zeros. * * Call this method before releasing references to buffers containing * sensitive data (keys, passwords, etc.) to minimize the window where * sensitive data remains in memory. * * Note: This is a best-effort security measure. JavaScript's JIT compiler * may optimize away the write if it detects the buffer isn't read afterward, * and copies of the data may exist elsewhere in memory. */ wipe() { this._buf.fill(0); } }