Helpers for 256/512 hashing (#1303)
* Helpers for 256/512 hashing * Adjust u8aToU8a (u8a-only check earlier) * Fix export * Simplify * revert
This commit is contained in:
@@ -3,7 +3,13 @@
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { hasBigInt, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
interface DualHash {
|
||||
256: (u8a: Uint8Array) => Uint8Array;
|
||||
512: (u8a: Uint8Array) => Uint8Array;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function createAsHex <T extends (...args: any[]) => Uint8Array> (fn: T): (...args: Parameters<T>) => HexString {
|
||||
@@ -15,3 +21,13 @@ export function createBitHasher <T> (bitLength: T, fn: (data: HexString | Buffer
|
||||
return (data: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
fn(data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
export function createDualHasher (wa: DualHash, js: DualHash): (value: HexString | Buffer | Uint8Array | string, bitLength?: 256 | 512, onlyJs?: boolean) => Uint8Array {
|
||||
return (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array => {
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? wa[bitLength](u8a)
|
||||
: js[bitLength](u8a);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,6 +9,16 @@ import { sha512 } from '@polkadot/x-noble-hashes/sha512';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
|
||||
const JS_HASH = {
|
||||
256: sha256,
|
||||
512: sha512
|
||||
};
|
||||
|
||||
const WA_MHAC = {
|
||||
256: hmacSha256,
|
||||
512: hmacSha512
|
||||
};
|
||||
|
||||
function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array {
|
||||
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
||||
@@ -19,16 +29,11 @@ function createSha (bitLength: BitLength): (key: Uint8Array | string, data: Uint
|
||||
* @description creates a Hmac Sha (256/512) Uint8Array from the key & data
|
||||
*/
|
||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8aKey = u8aToU8a(key);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? is256
|
||||
? hmacSha256(u8aKey, data)
|
||||
: hmacSha512(u8aKey, data)
|
||||
: is256
|
||||
? hmac(sha256, u8aKey, data)
|
||||
: hmac(sha512, u8aKey, data);
|
||||
? WA_MHAC[bitLength](u8aKey, data)
|
||||
: hmac(JS_HASH[bitLength], u8aKey, data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
import { keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
import { keccak_256 as keccak256Js, keccak_512 as keccak512Js } from '@polkadot/x-noble-hashes/sha3';
|
||||
|
||||
import { createAsHex, createBitHasher } from '../helpers';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
import { createAsHex, createBitHasher, createDualHasher } from '../helpers';
|
||||
|
||||
/**
|
||||
* @name keccakAsU8a
|
||||
@@ -25,18 +20,11 @@ type BitLength = 256 | 512;
|
||||
* keccakAsU8a('123'); // => Uint8Array
|
||||
* ```
|
||||
*/
|
||||
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? is256
|
||||
? keccak256(u8a)
|
||||
: keccak512(u8a)
|
||||
: is256
|
||||
? keccak256Js(u8a)
|
||||
: keccak512Js(u8a);
|
||||
}
|
||||
export const keccakAsU8a = createDualHasher(
|
||||
{ 256: keccak256, 512: keccak512 },
|
||||
{ 256: keccak256Js, 512: keccak512Js }
|
||||
);
|
||||
|
||||
/**
|
||||
* @name keccak256AsU8a
|
||||
|
||||
@@ -1,33 +1,20 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, sha256, sha512 } from '@polkadot/wasm-crypto';
|
||||
import { sha256, sha512 } from '@polkadot/wasm-crypto';
|
||||
import { sha256 as sha256Js } from '@polkadot/x-noble-hashes/sha256';
|
||||
import { sha512 as sha512Js } from '@polkadot/x-noble-hashes/sha512';
|
||||
|
||||
import { createBitHasher } from '../helpers';
|
||||
|
||||
type BitLength = 256 | 512;
|
||||
import { createBitHasher, createDualHasher } from '../helpers';
|
||||
|
||||
/**
|
||||
* @name shaAsU8a
|
||||
* @summary Creates a sha Uint8Array from the input.
|
||||
*/
|
||||
export function shaAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: BitLength = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !hasBigInt || (!onlyJs && isReady())
|
||||
? is256
|
||||
? sha256(u8a)
|
||||
: sha512(u8a)
|
||||
: is256
|
||||
? sha256Js(u8a)
|
||||
: sha512Js(u8a);
|
||||
}
|
||||
export const shaAsU8a = createDualHasher(
|
||||
{ 256: sha256, 512: sha512 },
|
||||
{ 256: sha256Js, 512: sha512Js }
|
||||
);
|
||||
|
||||
/**
|
||||
* @name sha256AsU8a
|
||||
|
||||
Reference in New Issue
Block a user