UNPKG

1.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.hmacSha512AsU8a = exports.hmacSha256AsU8a = exports.hmacShaAsU8a = void 0;
4const hmac_1 = require("@noble/hashes/hmac");
5const sha256_1 = require("@noble/hashes/sha256");
6const sha512_1 = require("@noble/hashes/sha512");
7const util_1 = require("@polkadot/util");
8const wasm_crypto_1 = require("@polkadot/wasm-crypto");
9const JS_HASH = {
10 256: sha256_1.sha256,
11 512: sha512_1.sha512
12};
13const WA_MHAC = {
14 256: wasm_crypto_1.hmacSha256,
15 512: wasm_crypto_1.hmacSha512
16};
17function createSha(bitLength) {
18 return (key, data, onlyJs) => hmacShaAsU8a(key, data, bitLength, onlyJs);
19}
20/**
21 * @name hmacShaAsU8a
22 * @description creates a Hmac Sha (256/512) Uint8Array from the key & data
23 */
24function hmacShaAsU8a(key, data, bitLength = 256, onlyJs) {
25 const u8aKey = (0, util_1.u8aToU8a)(key);
26 return !util_1.hasBigInt || (!onlyJs && (0, wasm_crypto_1.isReady)())
27 ? WA_MHAC[bitLength](u8aKey, data)
28 : (0, hmac_1.hmac)(JS_HASH[bitLength], u8aKey, data);
29}
30exports.hmacShaAsU8a = hmacShaAsU8a;
31/**
32 * @name hmacSha256AsU8a
33 * @description creates a Hmac Sha256 Uint8Array from the key & data
34 */
35exports.hmacSha256AsU8a = createSha(256);
36/**
37 * @name hmacSha512AsU8a
38 * @description creates a Hmac Sha512 Uint8Array from the key & data
39 */
40exports.hmacSha512AsU8a = createSha(512);