* @param {string} base32 - String to convert\r
* @returns {Uint8Array} Byte array representation of the input string\r
*/\r
- static toBytes (base32: string): Uint8Array {\r
+ static toBytes (base32: string): Uint8Array<ArrayBuffer> {\r
const leftover = (base32.length * 5) % 8\r
const offset = leftover === 0\r
? 0\r
*\r
* @param bytes - Buffer or bytes to erase\r
*/\r
- static erase (bytes: ArrayBuffer | Uint8Array<ArrayBuffer>): void {\r
+ static erase (bytes?: ArrayBuffer | Uint8Array<ArrayBuffer> | null): void {\r
+ if (bytes == null) return\r
if (bytes instanceof ArrayBuffer) {\r
if (bytes.detached) return\r
bytes = new Uint8Array(bytes)\r
* @param {number}[padding=0] - Minimum length of the resulting array padded as necessary with starting 0 values\r
* @returns {number[]} Decimal array representation of the input value\r
*/\r
- static toArray (bytes: Uint8Array, padding: number = 0): number[] {\r
+ static toArray (bytes: Uint8Array<ArrayBuffer>, padding: number = 0): number[] {\r
if (!(bytes instanceof Uint8Array)) {\r
throw new TypeError('bytes must be Uint8Array')\r
}\r
- return [...bytes.values()]\r
+ const byteArray = [...bytes.values()]\r
+ padding -= bytes.byteLength\r
+ if (padding > 0) {\r
+ const zeros = new Array(padding)\r
+ byteArray.splice(0, 0, ...zeros)\r
+ }\r
+ return byteArray\r
}\r
/**\r
* Converts a Uint8Aarray of bytes to a base32 string.\r
* @param {Uint8Array} bytes - Byte array to convert\r
* @returns {string} Binary string representation of the input value\r
*/\r
- static toBin (bytes: Uint8Array): string {\r
+ static toBin (bytes: Uint8Array<ArrayBuffer>): string {\r
return [...bytes].map(b => b.toString(2).padStart(8, '0')).join('')\r
}\r
\r
* @returns {string} Hexadecimal string representation of the input bytes\r
*/\r
static toHex (bytes: Uint8Array): string {\r
+ if (bytes.buffer instanceof ArrayBuffer && bytes.buffer.detached) return ''\r
const byteArray = [...bytes].map(byte => byte.toString(16).padStart(2, '0'))\r
return byteArray.join('').toUpperCase()\r
}\r
* @param {Uint8Array} bytes - Byte array to convert\r
* @returns {string} UTF-8 encoded text string\r
*/\r
- static toUtf8 (bytes: Uint8Array): string {\r
+ static toUtf8 (bytes: Uint8Array<ArrayBuffer>): string {\r
return new TextDecoder().decode(bytes)\r
}\r
}\r
* @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes\r
* @returns {Uint8Array} Byte array representation of the input decimal\r
*/\r
- static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array {\r
+ static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array<ArrayBuffer> {\r
if (decimal == null) {\r
throw new TypeError(`Failed to convert '${decimal}' from decimal to bytes`)\r
}\r