mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 11:27:59 +00:00
37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import { hmac } from '@noble/hashes/hmac';
|
|
import { sha256 } from '@noble/hashes/sha256';
|
|
import { sha512 } from '@noble/hashes/sha512';
|
|
import { hasBigInt, u8aToU8a } from '@pezkuwi/util';
|
|
import { hmacSha256, hmacSha512, isReady } from '@pezkuwi/wasm-crypto';
|
|
const JS_HASH = {
|
|
256: sha256,
|
|
512: sha512
|
|
};
|
|
const WA_MHAC = {
|
|
256: hmacSha256,
|
|
512: hmacSha512
|
|
};
|
|
function createSha(bitLength) {
|
|
return (key, data, onlyJs) => hmacShaAsU8a(key, data, bitLength, onlyJs);
|
|
}
|
|
/**
|
|
* @name hmacShaAsU8a
|
|
* @description creates a Hmac Sha (256/512) Uint8Array from the key & data
|
|
*/
|
|
export function hmacShaAsU8a(key, data, bitLength = 256, onlyJs) {
|
|
const u8aKey = u8aToU8a(key);
|
|
return !hasBigInt || (!onlyJs && isReady())
|
|
? WA_MHAC[bitLength](u8aKey, data)
|
|
: hmac(JS_HASH[bitLength], u8aKey, data);
|
|
}
|
|
/**
|
|
* @name hmacSha256AsU8a
|
|
* @description creates a Hmac Sha256 Uint8Array from the key & data
|
|
*/
|
|
export const hmacSha256AsU8a = /*#__PURE__*/ createSha(256);
|
|
/**
|
|
* @name hmacSha512AsU8a
|
|
* @description creates a Hmac Sha512 Uint8Array from the key & data
|
|
*/
|
|
export const hmacSha512AsU8a = /*#__PURE__*/ createSha(512);
|