From 2ec02e25803e20ea7cc43a26b1a90455ddc3a0c3 Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 19 Nov 2021 16:46:49 +0100 Subject: [PATCH] Combine sha256/512 into single top-level folder (#1218) * Combine sha256/512 into single top-level folder * linting * imports --- packages/util-crypto/src/bundle.ts | 3 +- packages/util-crypto/src/mnemonic/bip39.ts | 4 +-- packages/util-crypto/src/sha/asU8a.ts | 28 +++++++++++++++++ .../asU8a.spec.ts => sha/asU8a256.spec.ts} | 0 packages/util-crypto/src/sha/asU8a256.ts | 14 +++++++++ .../asU8a.spec.ts => sha/asU8a512.spec.ts} | 0 packages/util-crypto/src/sha/asU8a512.ts | 14 +++++++++ packages/util-crypto/src/sha/index.ts | 10 ++++++ packages/util-crypto/src/sha256/asU8a.ts | 21 ------------- packages/util-crypto/src/sha256/index.ts | 8 ----- packages/util-crypto/src/sha512/asU8a.ts | 31 ------------------- packages/util-crypto/src/sha512/index.ts | 8 ----- 12 files changed, 69 insertions(+), 72 deletions(-) create mode 100644 packages/util-crypto/src/sha/asU8a.ts rename packages/util-crypto/src/{sha256/asU8a.spec.ts => sha/asU8a256.spec.ts} (100%) create mode 100644 packages/util-crypto/src/sha/asU8a256.ts rename packages/util-crypto/src/{sha512/asU8a.spec.ts => sha/asU8a512.spec.ts} (100%) create mode 100644 packages/util-crypto/src/sha/asU8a512.ts create mode 100644 packages/util-crypto/src/sha/index.ts delete mode 100644 packages/util-crypto/src/sha256/asU8a.ts delete mode 100644 packages/util-crypto/src/sha256/index.ts delete mode 100644 packages/util-crypto/src/sha512/asU8a.ts delete mode 100644 packages/util-crypto/src/sha512/index.ts diff --git a/packages/util-crypto/src/bundle.ts b/packages/util-crypto/src/bundle.ts index ec9400b16..2f115ae87 100644 --- a/packages/util-crypto/src/bundle.ts +++ b/packages/util-crypto/src/bundle.ts @@ -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'; diff --git a/packages/util-crypto/src/mnemonic/bip39.ts b/packages/util-crypto/src/mnemonic/bip39.ts index 4c3788221..e8e667614 100644 --- a/packages/util-crypto/src/mnemonic/bip39.ts +++ b/packages/util-crypto/src/mnemonic/bip39.ts @@ -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'; diff --git a/packages/util-crypto/src/sha/asU8a.ts b/packages/util-crypto/src/sha/asU8a.ts new file mode 100644 index 000000000..bbdfa7326 --- /dev/null +++ b/packages/util-crypto/src/sha/asU8a.ts @@ -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() + ); +} diff --git a/packages/util-crypto/src/sha256/asU8a.spec.ts b/packages/util-crypto/src/sha/asU8a256.spec.ts similarity index 100% rename from packages/util-crypto/src/sha256/asU8a.spec.ts rename to packages/util-crypto/src/sha/asU8a256.spec.ts diff --git a/packages/util-crypto/src/sha/asU8a256.ts b/packages/util-crypto/src/sha/asU8a256.ts new file mode 100644 index 000000000..4ddd1a294 --- /dev/null +++ b/packages/util-crypto/src/sha/asU8a256.ts @@ -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); +} diff --git a/packages/util-crypto/src/sha512/asU8a.spec.ts b/packages/util-crypto/src/sha/asU8a512.spec.ts similarity index 100% rename from packages/util-crypto/src/sha512/asU8a.spec.ts rename to packages/util-crypto/src/sha/asU8a512.spec.ts diff --git a/packages/util-crypto/src/sha/asU8a512.ts b/packages/util-crypto/src/sha/asU8a512.ts new file mode 100644 index 000000000..1eee3fae7 --- /dev/null +++ b/packages/util-crypto/src/sha/asU8a512.ts @@ -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); +} diff --git a/packages/util-crypto/src/sha/index.ts b/packages/util-crypto/src/sha/index.ts new file mode 100644 index 000000000..d88c7db8a --- /dev/null +++ b/packages/util-crypto/src/sha/index.ts @@ -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'; diff --git a/packages/util-crypto/src/sha256/asU8a.ts b/packages/util-crypto/src/sha256/asU8a.ts deleted file mode 100644 index 12452833a..000000000 --- a/packages/util-crypto/src/sha256/asU8a.ts +++ /dev/null @@ -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() - ); -} diff --git a/packages/util-crypto/src/sha256/index.ts b/packages/util-crypto/src/sha256/index.ts deleted file mode 100644 index cd8d178e8..000000000 --- a/packages/util-crypto/src/sha256/index.ts +++ /dev/null @@ -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'; diff --git a/packages/util-crypto/src/sha512/asU8a.ts b/packages/util-crypto/src/sha512/asU8a.ts deleted file mode 100644 index ad09366f9..000000000 --- a/packages/util-crypto/src/sha512/asU8a.ts +++ /dev/null @@ -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 - *
- * - * ```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() - ); -} diff --git a/packages/util-crypto/src/sha512/index.ts b/packages/util-crypto/src/sha512/index.ts deleted file mode 100644 index 89679488f..000000000 --- a/packages/util-crypto/src/sha512/index.ts +++ /dev/null @@ -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';