npm package diff
Package: @noble/hashes
Versions: 1.3.2 - 1.3.3
File: package/utils.js
Index: package/utils.js
===================================================================
--- package/utils.js
+++ package/utils.js
@@ -6,33 +6,39 @@
// node.js versions earlier than v19 don't declare it in global scope.
// For node.js, package.json#exports field mapping rewrites import
// from `crypto` to `cryptoNode`, which imports native module.
// Makes the utils un-importable in browsers without a bundler.
-// Once node.js 18 is deprecated, we can just drop the import.
+// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
const crypto_1 = require("@noble/hashes/crypto");
-const u8a = (a) => a instanceof Uint8Array;
// Cast array to different type
const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
exports.u8 = u8;
const u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
exports.u32 = u32;
+function isBytes(a) {
+ return (a instanceof Uint8Array ||
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
+}
// Cast array to view
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
exports.createView = createView;
// The rotate right (circular right shift) operation for uint32
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
exports.rotr = rotr;
// big-endian hardware is rare. Just in case someone still decides to run hashes:
// early-throw an error because we don't support BE yet.
+// Other libraries would silently corrupt the data instead of throwing an error,
+// when they don't support it.
exports.isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
if (!exports.isLE)
throw new Error('Non little-endian hardware is not supported');
+// Array where index 0xf0 (240) is mapped to string 'f0'
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
/**
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
*/
function bytesToHex(bytes) {
- if (!u8a(bytes))
+ if (!isBytes(bytes))
throw new Error('Uint8Array expected');
// pre-caching improves the speed 6x
let hex = '';
for (let i = 0; i < bytes.length; i++) {
@@ -40,25 +46,38 @@
}
return hex;
}
exports.bytesToHex = bytesToHex;
+// We use optimized technique to convert hex string to byte array
+const asciis = { _0: 48, _9: 57, _A: 65, _F: 70, _a: 97, _f: 102 };
+function asciiToBase16(char) {
+ if (char >= asciis._0 && char <= asciis._9)
+ return char - asciis._0;
+ if (char >= asciis._A && char <= asciis._F)
+ return char - (asciis._A - 10);
+ if (char >= asciis._a && char <= asciis._f)
+ return char - (asciis._a - 10);
+ return;
+}
/**
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
*/
function hexToBytes(hex) {
if (typeof hex !== 'string')
throw new Error('hex string expected, got ' + typeof hex);
- const len = hex.length;
- if (len % 2)
- throw new Error('padded hex string expected, got unpadded hex of length ' + len);
- const array = new Uint8Array(len / 2);
- for (let i = 0; i < array.length; i++) {
- const j = i * 2;
- const hexByte = hex.slice(j, j + 2);
- const byte = Number.parseInt(hexByte, 16);
- if (Number.isNaN(byte) || byte < 0)
- throw new Error('Invalid byte sequence');
- array[i] = byte;
+ const hl = hex.length;
+ const al = hl / 2;
+ if (hl % 2)
+ throw new Error('padded hex string expected, got unpadded hex of length ' + hl);
+ const array = new Uint8Array(al);
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
+ const n1 = asciiToBase16(hex.charCodeAt(hi));
+ const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
+ if (n1 === undefined || n2 === undefined) {
+ const char = hex[hi] + hex[hi + 1];
+ throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
+ }
+ array[ai] = n1 * 16 + n2;
}
return array;
}
exports.hexToBytes = hexToBytes;
@@ -97,26 +116,31 @@
*/
function toBytes(data) {
if (typeof data === 'string')
data = utf8ToBytes(data);
- if (!u8a(data))
+ if (!isBytes(data))
throw new Error(`expected Uint8Array, got ${typeof data}`);
return data;
}
exports.toBytes = toBytes;
/**
* Copies several Uint8Arrays into one.
*/
function concatBytes(...arrays) {
- const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
- let pad = 0; // walk through each item, ensure they have proper type
- arrays.forEach((a) => {
- if (!u8a(a))
+ let sum = 0;
+ for (let i = 0; i < arrays.length; i++) {
+ const a = arrays[i];
+ if (!isBytes(a))
throw new Error('Uint8Array expected');
- r.set(a, pad);
+ sum += a.length;
+ }
+ const res = new Uint8Array(sum);
+ for (let i = 0, pad = 0; i < arrays.length; i++) {
+ const a = arrays[i];
+ res.set(a, pad);
pad += a.length;
- });
- return r;
+ }
+ return res;
}
exports.concatBytes = concatBytes;
// For runtime check if class implements interface
class Hash {