throw new TypeError('Invalid padding when converting hex to bytes', { cause: padding })
}
if (hex.length & 1) hex = `0${hex}`
- const hexArray = hex.match(/.{2}/g)
- if (hexArray == null) {
- throw new RangeError('Error converting hex string to bytes', { cause: hexArray })
- }
- const diff = padding - hexArray.length
+ const byteLength = hex.length >> 1
+ const diff = padding - byteLength
const offset = diff & ~(diff >> 31)
- const length = hexArray.length + offset
- const bytes = new Uint8Array(length)
+ const bytes = new Uint8Array(byteLength + offset)
bytes.set(hex.match(/.{2}/g)?.map(b => parseInt(b, 16)) ?? [], offset)
return bytes
},