Combine sha256/512 into single top-level folder (#1218)
* Combine sha256/512 into single top-level folder * linting * imports
This commit is contained in:
@@ -26,7 +26,6 @@ export * from './random';
|
||||
export * from './schnorrkel';
|
||||
export * from './scrypt';
|
||||
export * from './secp256k1';
|
||||
export * from './sha256';
|
||||
export * from './sha512';
|
||||
export * from './sha';
|
||||
export * from './signature';
|
||||
export * from './xxhash';
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
import { assert, stringToU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { pbkdf2Encode } from '../pbkdf2';
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { sha256AsU8a } from '../sha256/asU8a';
|
||||
import { randomAsU8a } from '../random';
|
||||
import { sha256AsU8a } from '../sha';
|
||||
import DEFAULT_WORDLIST from './bip39-en';
|
||||
|
||||
const INVALID_MNEMONIC = 'Invalid mnemonic';
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { u8aToU8a } from '@polkadot/util';
|
||||
import { isReady, sha256, sha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @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 {
|
||||
const is256 = bitLength === 256;
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return isReady() && !onlyJs
|
||||
? is256
|
||||
? sha256(u8a)
|
||||
: sha512(u8a)
|
||||
: new Uint8Array(
|
||||
is256
|
||||
? hash.sha256().update(u8a).digest()
|
||||
: hash.sha512().update(u8a).digest()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @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';
|
||||
@@ -1,21 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { isReady, sha256 } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name sha256AsU8a
|
||||
* @summary Creates sha-256 hash of the input.
|
||||
*/
|
||||
export function sha256AsU8a (data: Uint8Array, onlyJs = false): Uint8Array {
|
||||
return isReady() && !onlyJs
|
||||
? sha256(data)
|
||||
: new Uint8Array(
|
||||
hash
|
||||
.sha256()
|
||||
.update(data)
|
||||
.digest()
|
||||
);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
// 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';
|
||||
@@ -1,31 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { isReady, sha512 } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name sha512AsU8a
|
||||
* @summary Creates sha-512 hash of the input.
|
||||
* @description
|
||||
* Returns a sha-512 `Uint8Array` from the supplied data.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { sha512AsU8a } from '@polkadot/util-crypto';
|
||||
*
|
||||
* sha512AsU8a(Uint8Array.from([...])); // => Uint8Array([...])
|
||||
* ```
|
||||
*/
|
||||
export function sha512AsU8a (data: Uint8Array, onlyJs = false): Uint8Array {
|
||||
return isReady() && !onlyJs
|
||||
? sha512(data)
|
||||
: new Uint8Array(
|
||||
hash
|
||||
.sha512()
|
||||
.update(data)
|
||||
.digest()
|
||||
);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Implements Sha-512 hashing functions for a variety of input and outputs
|
||||
*/
|
||||
|
||||
export { sha512AsU8a } from './asU8a';
|
||||
Reference in New Issue
Block a user