@polkadot/util-crypto
Version:
A collection of useful crypto utilities for @polkadot
36 lines (35 loc) • 1.16 kB
JavaScript
import { hasBigInt, u8aToU8a } from '@polkadot/util';
import { isReady, twox } from '@polkadot/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 '@polkadot/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);