UNPKG

1.13 kBJavaScriptView Raw
1import { blake2b as blake2bJs } from '@noble/hashes/blake2b';
2import { hasBigInt, u8aToU8a } from '@polkadot/util';
3import { blake2b, isReady } from '@polkadot/wasm-crypto';
4import { createAsHex } from '../helpers.js';
5/**
6 * @name blake2AsU8a
7 * @summary Creates a blake2b u8a from the input.
8 * @description
9 * From a `Uint8Array` input, create the blake2b and return the result as a u8a with the specified `bitLength`.
10 * @example
11 * <BR>
12 *
13 * ```javascript
14 * import { blake2AsU8a } from '@polkadot/util-crypto';
15 *
16 * blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
17 * ```
18 */
19export function blake2AsU8a(data, bitLength = 256, key, onlyJs) {
20 const byteLength = Math.ceil(bitLength / 8);
21 const u8a = u8aToU8a(data);
22 return !hasBigInt || (!onlyJs && isReady())
23 ? blake2b(u8a, u8aToU8a(key), byteLength)
24 : key
25 ? blake2bJs(u8a, { dkLen: byteLength, key })
26 : blake2bJs(u8a, { dkLen: byteLength });
27}
28/**
29 * @name blake2AsHex
30 * @description Creates a blake2b hex from the input.
31 */
32export const blake2AsHex = /*#__PURE__*/ createAsHex(blake2AsU8a);