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