mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 06:47:57 +00:00
36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
import { hasBigInt, u8aToU8a } from '@pezkuwi/util';
|
|
import { isReady, twox } from '@pezkuwi/wasm-crypto';
|
|
import { createAsHex } from '../helpers.js';
|
|
import { xxhash64 } from './xxhash64.js';
|
|
/**
|
|
* @name xxhashAsU8a
|
|
* @summary Creates a xxhash64 u8a from the input.
|
|
* @description
|
|
* From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a `Uint8Array` with the specified `bitLength`.
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* import { xxhashAsU8a } from '@pezkuwi/util-crypto';
|
|
*
|
|
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
|
* ```
|
|
*/
|
|
export function xxhashAsU8a(data, bitLength = 64, onlyJs) {
|
|
const rounds = Math.ceil(bitLength / 64);
|
|
const u8a = u8aToU8a(data);
|
|
if (!hasBigInt || (!onlyJs && isReady())) {
|
|
return twox(u8a, rounds);
|
|
}
|
|
const result = new Uint8Array(rounds * 8);
|
|
for (let seed = 0; seed < rounds; seed++) {
|
|
result.set(xxhash64(u8a, seed).reverse(), seed * 8);
|
|
}
|
|
return result;
|
|
}
|
|
/**
|
|
* @name xxhashAsHex
|
|
* @description Creates a xxhash64 hex from the input.
|
|
*/
|
|
export const xxhashAsHex = /*#__PURE__*/ createAsHex(xxhashAsU8a);
|