123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.randomBytes = exports.wrapXOFConstructorWithOpts = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.checkOpts = exports.Hash = exports.concatBytes = exports.toBytes = exports.utf8ToBytes = exports.asyncLoop = exports.nextTick = exports.hexToBytes = exports.bytesToHex = exports.byteSwap32 = exports.byteSwapIfBE = exports.byteSwap = exports.isLE = exports.rotl = exports.rotr = exports.createView = exports.u32 = exports.u8 = exports.isBytes = void 0;
- const crypto_1 = require("@noble/hashes/crypto");
- const _assert_js_1 = require("./_assert.js");
- function isBytes(a) {
- return (a instanceof Uint8Array ||
- (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
- }
- exports.isBytes = isBytes;
- 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;
- const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
- exports.createView = createView;
- const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
- exports.rotr = rotr;
- const rotl = (word, shift) => (word << shift) | ((word >>> (32 - shift)) >>> 0);
- exports.rotl = rotl;
- exports.isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
- const byteSwap = (word) => ((word << 24) & 0xff000000) |
- ((word << 8) & 0xff0000) |
- ((word >>> 8) & 0xff00) |
- ((word >>> 24) & 0xff);
- exports.byteSwap = byteSwap;
- exports.byteSwapIfBE = exports.isLE ? (n) => n : (n) => (0, exports.byteSwap)(n);
- function byteSwap32(arr) {
- for (let i = 0; i < arr.length; i++) {
- arr[i] = (0, exports.byteSwap)(arr[i]);
- }
- }
- exports.byteSwap32 = byteSwap32;
- const hexes = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
- function bytesToHex(bytes) {
- (0, _assert_js_1.bytes)(bytes);
-
- let hex = '';
- for (let i = 0; i < bytes.length; i++) {
- hex += hexes[bytes[i]];
- }
- return hex;
- }
- exports.bytesToHex = bytesToHex;
- 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;
- }
- function hexToBytes(hex) {
- if (typeof hex !== 'string')
- throw new Error('hex string expected, got ' + typeof hex);
- 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;
- const nextTick = async () => { };
- exports.nextTick = nextTick;
- async function asyncLoop(iters, tick, cb) {
- let ts = Date.now();
- for (let i = 0; i < iters; i++) {
- cb(i);
-
- const diff = Date.now() - ts;
- if (diff >= 0 && diff < tick)
- continue;
- await (0, exports.nextTick)();
- ts += diff;
- }
- }
- exports.asyncLoop = asyncLoop;
- function utf8ToBytes(str) {
- if (typeof str !== 'string')
- throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
- return new Uint8Array(new TextEncoder().encode(str));
- }
- exports.utf8ToBytes = utf8ToBytes;
- function toBytes(data) {
- if (typeof data === 'string')
- data = utf8ToBytes(data);
- (0, _assert_js_1.bytes)(data);
- return data;
- }
- exports.toBytes = toBytes;
- function concatBytes(...arrays) {
- let sum = 0;
- for (let i = 0; i < arrays.length; i++) {
- const a = arrays[i];
- (0, _assert_js_1.bytes)(a);
- 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 res;
- }
- exports.concatBytes = concatBytes;
- class Hash {
-
- clone() {
- return this._cloneInto();
- }
- }
- exports.Hash = Hash;
- const toStr = {}.toString;
- function checkOpts(defaults, opts) {
- if (opts !== undefined && toStr.call(opts) !== '[object Object]')
- throw new Error('Options should be object or undefined');
- const merged = Object.assign(defaults, opts);
- return merged;
- }
- exports.checkOpts = checkOpts;
- function wrapConstructor(hashCons) {
- const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
- const tmp = hashCons();
- hashC.outputLen = tmp.outputLen;
- hashC.blockLen = tmp.blockLen;
- hashC.create = () => hashCons();
- return hashC;
- }
- exports.wrapConstructor = wrapConstructor;
- function wrapConstructorWithOpts(hashCons) {
- const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
- const tmp = hashCons({});
- hashC.outputLen = tmp.outputLen;
- hashC.blockLen = tmp.blockLen;
- hashC.create = (opts) => hashCons(opts);
- return hashC;
- }
- exports.wrapConstructorWithOpts = wrapConstructorWithOpts;
- function wrapXOFConstructorWithOpts(hashCons) {
- const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
- const tmp = hashCons({});
- hashC.outputLen = tmp.outputLen;
- hashC.blockLen = tmp.blockLen;
- hashC.create = (opts) => hashCons(opts);
- return hashC;
- }
- exports.wrapXOFConstructorWithOpts = wrapXOFConstructorWithOpts;
- function randomBytes(bytesLength = 32) {
- if (crypto_1.crypto && typeof crypto_1.crypto.getRandomValues === 'function') {
- return crypto_1.crypto.getRandomValues(new Uint8Array(bytesLength));
- }
- throw new Error('crypto.getRandomValues must be defined');
- }
- exports.randomBytes = randomBytes;
|