Provide JS/Wasm interfaces for hmacShaAsU8a (#1223)
* Provide JS/Wasm interfaces for hmacShaAsU8a * Adjust * Re-add helpers * Alignments
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
# CHANGELOG
|
||||
|
||||
## master
|
||||
|
||||
Changes:
|
||||
|
||||
- Provide JS/Wasm interfaces for `hmacShaAsU8a`
|
||||
|
||||
|
||||
## 7.9.1 Nov 21, 2021
|
||||
|
||||
Contributed:
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"@polkadot/networks": "7.9.1",
|
||||
"@polkadot/util": "7.9.1",
|
||||
"@polkadot/wasm-crypto": "^4.3.1",
|
||||
"@polkadot/wasm-crypto": "^4.3.2-0",
|
||||
"@polkadot/x-randomvalues": "7.9.1",
|
||||
"blakejs": "^1.1.1",
|
||||
"bn.js": "^4.12.0",
|
||||
|
||||
@@ -21,7 +21,7 @@ import { blake2AsU8a } from './asU8a';
|
||||
* blake2AsHex('abc'); // => 0xba80a53f981c4d0d
|
||||
* ```
|
||||
*/
|
||||
export function blake2AsHex (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs = false): HexString {
|
||||
export function blake2AsHex (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs?: boolean): HexString {
|
||||
return u8aToHex(
|
||||
blake2AsU8a(data, bitLength, key, onlyJs)
|
||||
);
|
||||
|
||||
@@ -22,7 +22,7 @@ import { blake2b, isReady } from '@polkadot/wasm-crypto';
|
||||
* blake2AsU8a('abc'); // => [0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d]
|
||||
* ```
|
||||
*/
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs = false): Uint8Array {
|
||||
export function blake2AsU8a (data: HexString | Uint8Array | string, bitLength = 256, key?: Uint8Array | null, onlyJs?: boolean): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { Keypair } from '../../types';
|
||||
import { assert, bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_32_OPTS } from '../../bn';
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
import { secp256k1KeypairFromSeed, secp256k1PrivateKeyTweakAdd } from '../../secp256k1';
|
||||
import { HARDENED, hdValidatePath } from '../validatePath';
|
||||
|
||||
@@ -31,7 +31,7 @@ function deriveChild (hd: CodedKeypair, index: number): CodedKeypair {
|
||||
: u8aConcat(hd.publicKey, indexBuffer);
|
||||
|
||||
try {
|
||||
const I = hmacSha512(hd.chainCode, data);
|
||||
const I = hmacShaAsU8a(hd.chainCode, data, 512);
|
||||
|
||||
return createCoded(
|
||||
secp256k1PrivateKeyTweakAdd(hd.secretKey, I.slice(0, 32)),
|
||||
@@ -44,7 +44,7 @@ function deriveChild (hd: CodedKeypair, index: number): CodedKeypair {
|
||||
}
|
||||
|
||||
export function hdEthereum (seed: Uint8Array, path = ''): Keypair {
|
||||
const I = hmacSha512(MASTER_SECRET, seed);
|
||||
const I = hmacShaAsU8a(MASTER_SECRET, seed, 512);
|
||||
let hd = createCoded(I.slice(0, 32), I.slice(32));
|
||||
|
||||
if (!path || path === 'm' || path === 'M' || path === "m'" || path === "M'") {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { BN_EIGHT, bnToU8a, u8aConcat, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { BN_LE_32_OPTS, BN_LE_512_OPTS, BN_LE_OPTS } from '../../bn';
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
|
||||
// performs hard-only derivation on the xprv
|
||||
export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Array {
|
||||
@@ -12,7 +12,7 @@ export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Arra
|
||||
const kr = xprv.subarray(32, 64);
|
||||
const cc = xprv.subarray(64, 96);
|
||||
const data = u8aConcat([0], kl, kr, bnToU8a(index, BN_LE_32_OPTS));
|
||||
const z = hmacSha512(cc, data);
|
||||
const z = hmacShaAsU8a(cc, data, 512);
|
||||
|
||||
data[0] = 0x01;
|
||||
|
||||
@@ -29,6 +29,6 @@ export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Arra
|
||||
),
|
||||
BN_LE_512_OPTS
|
||||
).subarray(0, 32),
|
||||
hmacSha512(cc, data).subarray(32, 64)
|
||||
hmacShaAsU8a(cc, data, 512).subarray(32, 64)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { hmacSha256, hmacSha512 } from '../../hmac';
|
||||
import { hmacShaAsU8a } from '../../hmac';
|
||||
import { mnemonicToSeedSync } from '../../mnemonic/bip39';
|
||||
|
||||
const ED25519_CRYPTO = 'ed25519 seed';
|
||||
@@ -11,11 +11,11 @@ const ED25519_CRYPTO = 'ed25519 seed';
|
||||
// gets an xprv from a mnemonic
|
||||
export function ledgerMaster (mnemonic: string, password?: string): Uint8Array {
|
||||
const seed = mnemonicToSeedSync(mnemonic, password);
|
||||
const chainCode = hmacSha256(ED25519_CRYPTO, new Uint8Array([1, ...seed]));
|
||||
const chainCode = hmacShaAsU8a(ED25519_CRYPTO, new Uint8Array([1, ...seed]), 256);
|
||||
let priv;
|
||||
|
||||
while (!priv || (priv[31] & 0b0010_0000)) {
|
||||
priv = hmacSha512(ED25519_CRYPTO, priv || seed);
|
||||
priv = hmacShaAsU8a(ED25519_CRYPTO, priv || seed, 512);
|
||||
}
|
||||
|
||||
priv[0] &= 0b1111_1000;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { hmacSha256 } from './sha256';
|
||||
export { hmacSha512 } from './sha512';
|
||||
export { hmacShaAsU8a, hmacSha256AsU8a, hmacSha512AsU8a } from './shaAsU8a';
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
export function hmacSha (key: Uint8Array | string, data: Uint8Array, is512: boolean): Uint8Array {
|
||||
return Uint8Array.from(
|
||||
hash
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.hmac(is512 ? hash.sha512 : hash.sha256, key)
|
||||
.update(data)
|
||||
.digest());
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { hmacSha256 } from '.';
|
||||
|
||||
describe('hmacSha256', (): void => {
|
||||
it('has a known output', (): void => {
|
||||
const key = stringToU8a('secret');
|
||||
const data = stringToU8a('some message');
|
||||
|
||||
expect(
|
||||
u8aToHex(
|
||||
hmacSha256(key, data)
|
||||
)
|
||||
).toEqual(
|
||||
'0xf28a70b41263840e5c059a0a733336e0957efba87902aa8cca11441d4b0c96d7'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hmacSha } from './sha';
|
||||
|
||||
export function hmacSha256 (key: Uint8Array | string, data: Uint8Array): Uint8Array {
|
||||
return hmacSha(key, data, false);
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { hmacSha512 } from '.';
|
||||
|
||||
describe('hmacSha512', (): void => {
|
||||
it('has a known output', (): void => {
|
||||
const key = stringToU8a('secret');
|
||||
const data = stringToU8a('some message');
|
||||
|
||||
expect(
|
||||
u8aToHex(
|
||||
hmacSha512(key, data)
|
||||
)
|
||||
).toEqual(
|
||||
'0x295832e97ed77be75a9fa98029497e4a722c4b9a2f21b39d34f1befa931a39ec520fd24711d6f5c03501384ea66b83066a01a82c57a0460f8cd1f471fcce5841'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hmacSha } from './sha';
|
||||
|
||||
export function hmacSha512 (key: Uint8Array | string, data: Uint8Array): Uint8Array {
|
||||
return hmacSha(key, data, true);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a, stringToU8a } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { performanceTest } from '../../test/performance';
|
||||
import { hmacShaAsU8a } from '.';
|
||||
|
||||
describe('hmacShaAsU8a', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
const key = stringToU8a('secret');
|
||||
const data = stringToU8a('some message');
|
||||
const output = {
|
||||
256: hexToU8a(
|
||||
'0xf28a70b41263840e5c059a0a733336e0957efba87902aa8cca11441d4b0c96d7'
|
||||
),
|
||||
512: hexToU8a(
|
||||
'0x295832e97ed77be75a9fa98029497e4a722c4b9a2f21b39d34f1befa931a39ec520fd24711d6f5c03501384ea66b83066a01a82c57a0460f8cd1f471fcce5841'
|
||||
)
|
||||
};
|
||||
|
||||
describe.each([256, 512] as (256 | 512)[])('bitLength=$p', (bitLength): void => {
|
||||
describe.each([false, true])('onlyJs=%p', (onlyJs): void => {
|
||||
it('returns an hex representation (string)', (): void => {
|
||||
expect(
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs)
|
||||
).toEqual(output[bitLength]);
|
||||
});
|
||||
});
|
||||
|
||||
performanceTest(`hmacShaAsU8a, bitLength=${bitLength}`, 16000, (input, onlyJs) =>
|
||||
hmacShaAsU8a(input, input, bitLength, onlyJs)
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { hmacSha256, hmacSha512, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
type HashFn = (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean) => Uint8Array;
|
||||
|
||||
function createSha (bitLength: 256 | 512 = 256): HashFn {
|
||||
return (key: Uint8Array | string, data: Uint8Array, onlyJs?: boolean): Uint8Array =>
|
||||
hmacShaAsU8a(key, data, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
export function hmacShaAsU8a (key: Uint8Array | string, data: Uint8Array, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8aKey = u8aToU8a(key);
|
||||
|
||||
return isReady() && !onlyJs
|
||||
? is256
|
||||
? hmacSha256(u8aKey, data)
|
||||
: hmacSha512(u8aKey, data)
|
||||
: new Uint8Array(
|
||||
is256
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
? hash.hmac(hash.sha256, u8aKey).update(data).digest()
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
: hash.hmac(hash.sha512, u8aKey).update(data).digest()
|
||||
);
|
||||
}
|
||||
|
||||
export const hmacSha256AsU8a = createSha(256);
|
||||
export const hmacSha512AsU8a = createSha(512);
|
||||
@@ -8,6 +8,13 @@ import js from 'js-sha3';
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
type HashFn = (value: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array;
|
||||
|
||||
function createKeccak (bitLength: 256 | 512 = 256): HashFn {
|
||||
return (value: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
keccakAsU8a(value, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name keccakAsU8a
|
||||
* @summary Creates a keccak Uint8Array from the input.
|
||||
@@ -22,7 +29,7 @@ import { isReady, keccak256, keccak512 } from '@polkadot/wasm-crypto';
|
||||
* keccakAsU8a('123'); // => Uint8Array
|
||||
* ```
|
||||
*/
|
||||
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs = false): Uint8Array {
|
||||
export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
@@ -36,3 +43,6 @@ export function keccakAsU8a (value: HexString | Buffer | Uint8Array | string, bi
|
||||
: js.keccak512.update(u8a).arrayBuffer()
|
||||
);
|
||||
}
|
||||
|
||||
export const keccak256AsU8a = createKeccak(256);
|
||||
export const keccak512AsU8a = createKeccak(512);
|
||||
|
||||
@@ -6,4 +6,4 @@
|
||||
*/
|
||||
|
||||
export { keccakAsHex } from './asHex';
|
||||
export { keccakAsU8a } from './asU8a';
|
||||
export { keccakAsU8a, keccak256AsU8a, keccak512AsU8a } from './asU8a';
|
||||
|
||||
@@ -28,7 +28,7 @@ const STRENGTH_MAP = {
|
||||
* const mnemonic = mnemonicGenerate(); // => string
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs = false): string {
|
||||
export function mnemonicGenerate (numWords: WordCount = 12, onlyJs?: boolean): string {
|
||||
return isReady() && !onlyJs
|
||||
? bip39Generate(numWords)
|
||||
: generateMnemonic(STRENGTH_MAP[numWords]);
|
||||
|
||||
@@ -5,7 +5,7 @@ import { bip39ToEntropy, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { mnemonicToEntropy as jsToEntropy } from './bip39';
|
||||
|
||||
export function mnemonicToEntropy (mnemonic: string, onlyJs = false): Uint8Array {
|
||||
export function mnemonicToEntropy (mnemonic: string, onlyJs?: boolean): Uint8Array {
|
||||
return isReady() && !onlyJs
|
||||
? bip39ToEntropy(mnemonic)
|
||||
: jsToEntropy(mnemonic);
|
||||
|
||||
@@ -24,7 +24,7 @@ import { mnemonicValidate } from './validate';
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs = false, byteLength: 32 | 64 = 32): Uint8Array {
|
||||
export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs?: boolean, byteLength: 32 | 64 = 32): Uint8Array {
|
||||
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
|
||||
assert([32, 64].includes(byteLength), () => `Invalid seed length ${byteLength}, expected 32 or 64`);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import { pbkdf2Encode } from '../pbkdf2';
|
||||
import { mnemonicToEntropy } from './toEntropy';
|
||||
import { mnemonicValidate } from './validate';
|
||||
|
||||
export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs = false): Uint8Array {
|
||||
export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs?: boolean): Uint8Array {
|
||||
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
|
||||
|
||||
if (isReady() && !onlyJs) {
|
||||
|
||||
@@ -18,7 +18,7 @@ import { validateMnemonic } from './bip39';
|
||||
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
||||
* ```
|
||||
*/
|
||||
export function mnemonicValidate (mnemonic: string, onlyJs = false): boolean {
|
||||
export function mnemonicValidate (mnemonic: string, onlyJs?: boolean): boolean {
|
||||
return isReady() && !onlyJs
|
||||
? bip39Validate(mnemonic)
|
||||
: validateMnemonic(mnemonic);
|
||||
|
||||
@@ -21,7 +21,7 @@ import { ed25519KeypairFromSeed, isReady } from '@polkadot/wasm-crypto';
|
||||
* naclKeypairFromSeed(...); // => { secretKey: [...], publicKey: [...] }
|
||||
* ```
|
||||
*/
|
||||
export function naclKeypairFromSeed (seed: Uint8Array, onlyJs = false): Keypair {
|
||||
export function naclKeypairFromSeed (seed: Uint8Array, onlyJs?: boolean): Keypair {
|
||||
if (!onlyJs && isReady()) {
|
||||
const full = ed25519KeypairFromSeed(seed);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import { ed25519Sign, isReady } from '@polkadot/wasm-crypto';
|
||||
* naclSign([...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclSign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>, onlyJs = false): Uint8Array {
|
||||
export function naclSign (message: HexString | Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>, onlyJs?: boolean): Uint8Array {
|
||||
assert(secretKey, 'Expected a valid secretKey');
|
||||
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
@@ -22,7 +22,7 @@ import { ed25519Verify, isReady } from '@polkadot/wasm-crypto';
|
||||
* naclVerify([...], [...], [...]); // => true/false
|
||||
* ```
|
||||
*/
|
||||
export function naclVerify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string, onlyJs = false): boolean {
|
||||
export function naclVerify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string, onlyJs?: boolean): boolean {
|
||||
const messageU8a = u8aToU8a(message);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
@@ -15,7 +15,7 @@ interface Result {
|
||||
salt: Uint8Array;
|
||||
}
|
||||
|
||||
export function pbkdf2Encode (passphrase?: HexString | Buffer | Uint8Array | string, salt: Buffer | Uint8Array = randomAsU8a(), rounds = 2048, onlyJs = false): Result {
|
||||
export function pbkdf2Encode (passphrase?: HexString | Buffer | Uint8Array | string, salt: Buffer | Uint8Array = randomAsU8a(), rounds = 2048, onlyJs?: boolean): Result {
|
||||
const u8aPass = u8aToU8a(passphrase);
|
||||
const u8aSalt = u8aToU8a(salt);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import { bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { BN_BE_32_OPTS } from '../bn';
|
||||
import { hmacSha512 } from '../hmac';
|
||||
import { hmacShaAsU8a } from '../hmac';
|
||||
|
||||
export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array {
|
||||
let out = new Uint8Array();
|
||||
@@ -17,12 +17,12 @@ export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: numb
|
||||
num++;
|
||||
block.set(bnToU8a(num, BN_BE_32_OPTS), salt.length);
|
||||
|
||||
let prev = hmacSha512(password, block);
|
||||
let prev = hmacShaAsU8a(password, block, 512);
|
||||
const md = prev;
|
||||
let i = 0;
|
||||
|
||||
while (++i < rounds) {
|
||||
prev = hmacSha512(password, prev);
|
||||
prev = hmacShaAsU8a(password, prev, 512);
|
||||
|
||||
let j = -1;
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ interface Result {
|
||||
salt: Uint8Array;
|
||||
}
|
||||
|
||||
export function scryptEncode (passphrase?: HexString | Uint8Array | string, salt = randomAsU8a(), params = DEFAULT_PARAMS, onlyJs = false): Result {
|
||||
export function scryptEncode (passphrase?: HexString | Uint8Array | string, salt = randomAsU8a(), params = DEFAULT_PARAMS, onlyJs?: boolean): Result {
|
||||
const u8a = u8aToU8a(passphrase);
|
||||
|
||||
const password = isReady() && !onlyJs
|
||||
|
||||
@@ -8,11 +8,18 @@ import hash from 'hash.js';
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, sha256, sha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
type HashFn = (value: HexString | Buffer | Uint8Array | string, onlyJs?: boolean) => Uint8Array;
|
||||
|
||||
function createSha (bitLength: 256 | 512 = 256): HashFn {
|
||||
return (value: HexString | Buffer | Uint8Array | string, onlyJs?: boolean): Uint8Array =>
|
||||
shaAsU8a(value, bitLength, onlyJs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name shaAsU8a
|
||||
* @summary Creates a sha Uint8Array from the input.
|
||||
*/
|
||||
export function shaAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs = false): Uint8Array {
|
||||
export function shaAsU8a (value: HexString | Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs?: boolean): Uint8Array {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
@@ -26,3 +33,6 @@ export function shaAsU8a (value: HexString | Buffer | Uint8Array | string, bitLe
|
||||
: hash.sha512().update(u8a).digest()
|
||||
);
|
||||
}
|
||||
|
||||
export const sha256AsU8a = createSha(256);
|
||||
export const sha512AsU8a = createSha(512);
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { shaAsU8a } from './asU8a';
|
||||
|
||||
/**
|
||||
* @name sha256AsU8a
|
||||
* @summary Creates a sha256 Uint8Array from the input.
|
||||
*/
|
||||
export function sha256AsU8a (value: HexString | Buffer | Uint8Array | string, onlyJs = false): Uint8Array {
|
||||
return shaAsU8a(value, 256, onlyJs);
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { shaAsU8a } from './asU8a';
|
||||
|
||||
/**
|
||||
* @name sha512AsU8a
|
||||
* @summary Creates a sha256 Uint8Array from the input.
|
||||
*/
|
||||
export function sha512AsU8a (value: HexString | Buffer | Uint8Array | string, onlyJs = false): Uint8Array {
|
||||
return shaAsU8a(value, 512, onlyJs);
|
||||
}
|
||||
@@ -5,6 +5,4 @@
|
||||
* @summary Implements Sha-256/512 hashing functions for a variety of input and outputs
|
||||
*/
|
||||
|
||||
export { shaAsU8a } from './asU8a';
|
||||
export { sha256AsU8a } from './asU8a256';
|
||||
export { sha512AsU8a } from './asU8a512';
|
||||
export { shaAsU8a, sha256AsU8a, sha512AsU8a } from './asU8a';
|
||||
|
||||
@@ -22,7 +22,7 @@ import xxhash64AsBn from './xxhash64/asBn';
|
||||
* xxhashAsU8a('abc'); // => 0x44bc2cf5ad770999
|
||||
* ```
|
||||
*/
|
||||
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength = 64, onlyJs = false): Uint8Array {
|
||||
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength = 64, onlyJs?: boolean): Uint8Array {
|
||||
const iterations = Math.ceil(bitLength / 64);
|
||||
const u8a = u8aToU8a(data);
|
||||
|
||||
|
||||
@@ -2086,7 +2086,7 @@ __metadata:
|
||||
"@babel/runtime": ^7.16.3
|
||||
"@polkadot/networks": 7.9.1
|
||||
"@polkadot/util": 7.9.1
|
||||
"@polkadot/wasm-crypto": ^4.3.1
|
||||
"@polkadot/wasm-crypto": ^4.3.2-0
|
||||
"@polkadot/x-randomvalues": 7.9.1
|
||||
"@types/bn.js": ^4.11.6
|
||||
"@types/create-hash": ^1.2.2
|
||||
@@ -2124,35 +2124,35 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@polkadot/wasm-crypto-asmjs@npm:^4.3.1":
|
||||
version: 4.3.1
|
||||
resolution: "@polkadot/wasm-crypto-asmjs@npm:4.3.1"
|
||||
"@polkadot/wasm-crypto-asmjs@npm:^4.3.2-0":
|
||||
version: 4.3.2-0
|
||||
resolution: "@polkadot/wasm-crypto-asmjs@npm:4.3.2-0"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.16.3
|
||||
checksum: 145bff607c0a1da592d9945a902d5ea2373487a14149ed7fb940151c6977660c935d67bf90e06ea39affe3d72e4483a1b66d758447385039b41f45935db8e66a
|
||||
checksum: 922d00e615396d4686b68e1f2514363ae8a52db4ac0df95d9919b5a8abce9d1f29fb6abfd6e3f48069d5dc4f8028650a7da099371ca03886f42395d46767f126
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/wasm-crypto-wasm@npm:^4.3.1":
|
||||
version: 4.3.1
|
||||
resolution: "@polkadot/wasm-crypto-wasm@npm:4.3.1"
|
||||
"@polkadot/wasm-crypto-wasm@npm:^4.3.2-0":
|
||||
version: 4.3.2-0
|
||||
resolution: "@polkadot/wasm-crypto-wasm@npm:4.3.2-0"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.16.3
|
||||
checksum: e06c91e267790e100f5c36ff39bc226323cfc19600920ee2dff77e38251b28c8010d4b58372aaaffb60a5611dfc9b904e36c87ace2a40cf1bbee8446ff7282b5
|
||||
checksum: db46ed3e1b25dcdd111cb0ddd9d4fe3770601d313605c20e154539b5484046606241cf77914932771afc12df544e6ca66f87af38f4508b885f2c921b166733bf
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/wasm-crypto@npm:^4.3.1":
|
||||
version: 4.3.1
|
||||
resolution: "@polkadot/wasm-crypto@npm:4.3.1"
|
||||
"@polkadot/wasm-crypto@npm:^4.3.2-0":
|
||||
version: 4.3.2-0
|
||||
resolution: "@polkadot/wasm-crypto@npm:4.3.2-0"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.16.3
|
||||
"@polkadot/wasm-crypto-asmjs": ^4.3.1
|
||||
"@polkadot/wasm-crypto-wasm": ^4.3.1
|
||||
"@polkadot/wasm-crypto-asmjs": ^4.3.2-0
|
||||
"@polkadot/wasm-crypto-wasm": ^4.3.2-0
|
||||
peerDependencies:
|
||||
"@polkadot/util": "*"
|
||||
"@polkadot/x-randomvalues": "*"
|
||||
checksum: 76bf64f148b3c8fb32a57ffdd982f88901d6aa17beb685fe79f7a3a0268202cce9da72659539964882c325eef3b04ab01e847f0090c064f412a09a2fb81399de
|
||||
checksum: 7cb72df43e8dae651fd9bf9088e1d44958150cfe3c5c2a49a473270b545446b63a8458c9d0650005d1e612210c3290891fe9c3575c3cd9039c910a66fb1ac3ad
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user