1 | import { hmac } from '@noble/hashes/hmac';
|
2 | import { sha256 } from '@noble/hashes/sha256';
|
3 | import { sha512 } from '@noble/hashes/sha512';
|
4 | import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
5 | import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
|
6 | const JS_HASH = {
|
7 | 256: sha256,
|
8 | 512: sha512
|
9 | };
|
10 | const WA_MHAC = {
|
11 | 256: hmacSha256,
|
12 | 512: hmacSha512
|
13 | };
|
14 | function createSha(bitLength) {
|
15 | return (key, data, onlyJs) => hmacShaAsU8a(key, data, bitLength, onlyJs);
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export function hmacShaAsU8a(key, data, bitLength = 256, onlyJs) {
|
22 | const u8aKey = u8aToU8a(key);
|
23 | return !hasBigInt || (!onlyJs && isReady())
|
24 | ? WA_MHAC[bitLength](u8aKey, data)
|
25 | : hmac(JS_HASH[bitLength], u8aKey, data);
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export const hmacSha256AsU8a = createSha(256);
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export const hmacSha512AsU8a = createSha(512);
|