From 862cd5bb100935f08fbe1e5deebf81645505a529 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 15 Jul 2025 13:42:07 -0700 Subject: [PATCH] Fix conversion of detached buffers to hexadecimal. --- src/lib/convert.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/lib/convert.ts b/src/lib/convert.ts index 5c347ae..ab35a73 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -10,7 +10,7 @@ export class base32 { * @param {string} base32 - String to convert * @returns {Uint8Array} Byte array representation of the input string */ - static toBytes (base32: string): Uint8Array { + static toBytes (base32: string): Uint8Array { const leftover = (base32.length * 5) % 8 const offset = leftover === 0 ? 0 @@ -82,7 +82,8 @@ export class bytes { * * @param bytes - Buffer or bytes to erase */ - static erase (bytes: ArrayBuffer | Uint8Array): void { + static erase (bytes?: ArrayBuffer | Uint8Array | null): void { + if (bytes == null) return if (bytes instanceof ArrayBuffer) { if (bytes.detached) return bytes = new Uint8Array(bytes) @@ -99,11 +100,17 @@ export class bytes { * @param {number}[padding=0] - Minimum length of the resulting array padded as necessary with starting 0 values * @returns {number[]} Decimal array representation of the input value */ - static toArray (bytes: Uint8Array, padding: number = 0): number[] { + static toArray (bytes: Uint8Array, padding: number = 0): number[] { if (!(bytes instanceof Uint8Array)) { throw new TypeError('bytes must be Uint8Array') } - return [...bytes.values()] + const byteArray = [...bytes.values()] + padding -= bytes.byteLength + if (padding > 0) { + const zeros = new Array(padding) + byteArray.splice(0, 0, ...zeros) + } + return byteArray } /** * Converts a Uint8Aarray of bytes to a base32 string. @@ -139,7 +146,7 @@ export class bytes { * @param {Uint8Array} bytes - Byte array to convert * @returns {string} Binary string representation of the input value */ - static toBin (bytes: Uint8Array): string { + static toBin (bytes: Uint8Array): string { return [...bytes].map(b => b.toString(2).padStart(8, '0')).join('') } @@ -171,6 +178,7 @@ export class bytes { * @returns {string} Hexadecimal string representation of the input bytes */ static toHex (bytes: Uint8Array): string { + if (bytes.buffer instanceof ArrayBuffer && bytes.buffer.detached) return '' const byteArray = [...bytes].map(byte => byte.toString(16).padStart(2, '0')) return byteArray.join('').toUpperCase() } @@ -181,7 +189,7 @@ export class bytes { * @param {Uint8Array} bytes - Byte array to convert * @returns {string} UTF-8 encoded text string */ - static toUtf8 (bytes: Uint8Array): string { + static toUtf8 (bytes: Uint8Array): string { return new TextDecoder().decode(bytes) } } @@ -214,7 +222,7 @@ export class dec { * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes * @returns {Uint8Array} Byte array representation of the input decimal */ - static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array { + static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array { if (decimal == null) { throw new TypeError(`Failed to convert '${decimal}' from decimal to bytes`) } -- 2.47.3