Add/use sha256 export (#1215)
This commit is contained in:
@@ -14,6 +14,7 @@ Changes:
|
||||
- Add `stringPascalCase` utility
|
||||
- Add `isCodec` to check for Codec-like structures
|
||||
- Add `has*` detection shortcuts (e.g. `hasWasm` or `hasBuffer`)
|
||||
- Add `sha256AsU8a` util (& replace use internally)
|
||||
- Optimize for `hexToU8a` and `u8aToHex` functions
|
||||
- Optimize `u8aEq` to use `DataView` for compares
|
||||
- Align base{32, 58, 64} around (audited) `micro-base`
|
||||
|
||||
@@ -26,6 +26,7 @@ export * from './random';
|
||||
export * from './schnorrkel';
|
||||
export * from './scrypt';
|
||||
export * from './secp256k1';
|
||||
export * from './sha256';
|
||||
export * from './sha512';
|
||||
export * from './signature';
|
||||
export * from './xxhash';
|
||||
|
||||
@@ -13,12 +13,11 @@
|
||||
// - Remove setting of wordlist passing of wordlist in functions
|
||||
// - Remove mnemonicToSeed (we only use the sync variant)
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { assert, stringToU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { pbkdf2Encode } from '../pbkdf2';
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { sha256AsU8a } from '../sha256/asU8a';
|
||||
import DEFAULT_WORDLIST from './bip39-en';
|
||||
|
||||
const INVALID_MNEMONIC = 'Invalid mnemonic';
|
||||
@@ -39,9 +38,7 @@ function bytesToBinary (bytes: number[]): string {
|
||||
|
||||
function deriveChecksumBits (entropyBuffer: Uint8Array): string {
|
||||
return bytesToBinary(
|
||||
Array.from(
|
||||
hash.sha256().update(entropyBuffer).digest()
|
||||
)
|
||||
Array.from(sha256AsU8a(entropyBuffer))
|
||||
).slice(0, (entropyBuffer.length * 8) / 32);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
|
||||
import { sha256AsU8a } from '.';
|
||||
|
||||
const TESTS = [
|
||||
{
|
||||
input: '0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
|
||||
output: '0x5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456'
|
||||
},
|
||||
{
|
||||
input: '0x7941cb07924fdc7b710e11d98d82850e89566e1c3cb980517ffe4b430f86dfd5',
|
||||
output: '0xe668668fa102ccd4d3cf50e4472be8426e7dc52d6d355bee8c5e2177d1de81f7'
|
||||
},
|
||||
{
|
||||
input: '0x5d9a905bc3ca7c153623af0a05231960eb3f5bdd5b8b7b55ede8b2deac689d65',
|
||||
output: '0x6721bc38e80d03f4f45f1544819d17e0c33674eb6efcddf6c6192cd78194dd9f'
|
||||
},
|
||||
{
|
||||
input: '0xb0cd6528ae4d5baf7c6f0d202e7f372b6488d22b2a5bd72f794f8f1e6031b31f',
|
||||
output: '0x353f5a030af27885aed3571cd9e2cac9f4331c51d31cef496a586496aec32256'
|
||||
},
|
||||
{
|
||||
input: '0x841267bd4110e1b634e17cd019abc6ae4f9a6dd097fb063000c1615a643b5463',
|
||||
output: '0x8174a30d921978e7055516189872cca737b49468c6bcf7fdfe59d4a54c489a1f'
|
||||
}
|
||||
];
|
||||
|
||||
describe('sha256AsU8a', (): void => {
|
||||
it.each(TESTS)('creates known sha-256 hash', ({ input, output }): void => {
|
||||
expect(
|
||||
sha256AsU8a(hexToU8a(input))
|
||||
).toEqual(hexToU8a(output));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
/**
|
||||
* @name sha256AsU8a
|
||||
* @summary Creates sha-256 hash of the input.
|
||||
*/
|
||||
export function sha256AsU8a (data: Uint8Array): Uint8Array {
|
||||
return new Uint8Array(
|
||||
hash
|
||||
.sha256()
|
||||
.update(data)
|
||||
.digest()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Implements Sha-256 hashing functions for a variety of input and outputs
|
||||
*/
|
||||
|
||||
export { sha256AsU8a } from './asU8a';
|
||||
Reference in New Issue
Block a user