Add keccak512 (#820)

* Add keccak512

* dedupe

* Adjust hex test

* ss58

* fix expectation
This commit is contained in:
Jaco Greeff
2020-12-25 09:52:49 +01:00
committed by GitHub
parent dbf1837b97
commit 0d7ed7420d
6 changed files with 56 additions and 31 deletions
+4 -1
View File
@@ -6,7 +6,8 @@ Changes:
- Add `arrayFlatten` to merge a series of arrays into a single
- Add `arrayChunk` to chunk an array into multiples of defined size
- Add `bitLength` (256/512) to `keccakAs{Hex, U8a}`
- Align `@polkadot/networks` with Substrate master
## 5.1.1 Dec 19, 2020
@@ -15,6 +16,7 @@ Changes:
- `detectPackage` now can take a path-retrieval callback as second argument
- Adjust `blake2js` imports to consistently cater for esm & cjs usage
- Explicitly mark `detectPackage` usage as having side-effects
- Align `@polkadot/networks` with Substrate master
- `@polkadot/wasm-crypto` 3.1.1
@@ -30,6 +32,7 @@ Changes:
- Debug logs now don't depend on `NODE_ENV`, but rather `DEBUG=<type,type,...>` (`*` logs all)
- Limit debug console output via `DEBUG_MAX=<number>` value, trimming when passed
- Add `memoize` util, allowing for function memoization (as used in the API)
- Align `@polkadot/networks` with Substrate master
- `@polkadot/wasm-crypto` 3.0.1
+13 -6
View File
@@ -5,17 +5,24 @@ import { waitReady } from '@polkadot/wasm-crypto';
import { keccakAsHex } from '.';
const value = 'test value';
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
const BITS: (256 | 512)[] = [256, 512];
const value = 'test';
const result = {
256: '9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658',
512: '1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e'
};
describe('keccakAsHex', (): void => {
beforeEach(async (): Promise<void> => {
await waitReady();
});
it('returns a prefixed hex representation', (): void => {
expect(
keccakAsHex(value)
).toEqual(`0x${result}`);
BITS.forEach((bitLength): void => {
it('returns a prefixed hex representation', (): void => {
expect(
keccakAsHex(value, bitLength)
).toEqual(`0x${result[bitLength]}`);
});
});
});
+2 -2
View File
@@ -19,8 +19,8 @@ import { keccakAsU8a } from './asU8a';
* keccakAsHex('123'); // => 0x...
* ```
*/
export function keccakAsHex (value: Buffer | Uint8Array | string): string {
export function keccakAsHex (value: Buffer | Uint8Array | string, bitLength?: 256 | 512): string {
return u8aToHex(
keccakAsU8a(value)
keccakAsU8a(value, bitLength)
);
}
+31 -18
View File
@@ -6,37 +6,50 @@ import { waitReady } from '@polkadot/wasm-crypto';
import { keccakAsU8a } from '.';
const BITS: (256 | 512)[] = [256, 512];
describe('keccakAsU8a', (): void => {
beforeEach(async (): Promise<void> => {
await waitReady();
});
const input = 'test value';
const output = hexToU8a(
'0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'
);
const output = {
256: hexToU8a(
'0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e'
),
512: hexToU8a(
'0xc1b50cc57f85ccd968a9d7c7a809dcebd140a548c8e0b67f3afcdd6fc14cca2b1d04187aef24ba0081b74f2ec362431e425760febe94a5607790854cafe5b197'
)
};
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (string, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(input, onlyJs)
).toEqual(output);
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (string, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(input, bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Buffer, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(Buffer.from(input), onlyJs)
).toEqual(output);
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Buffer, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(Buffer.from(input), bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Uint8Array, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(stringToU8a(input), onlyJs)
).toEqual(output);
BITS.forEach((bitLength): void => {
[false, true].forEach((onlyJs): void => {
it(`returns an hex representation (Uint8Array, bitLength=${bitLength}, onlyJs=${onlyJs.toString()})`, (): void => {
expect(
keccakAsU8a(stringToU8a(input), bitLength, onlyJs)
).toEqual(output[bitLength]);
});
});
});
});
+5 -3
View File
@@ -20,10 +20,12 @@ import { isReady, keccak256 } from '@polkadot/wasm-crypto';
* keccakAsU8a('123'); // => Uint8Array
* ```
*/
export function keccakAsU8a (value: Buffer | Uint8Array | string, onlyJs = false): Uint8Array {
return isReady() && !onlyJs
export function keccakAsU8a (value: Buffer | Uint8Array | string, bitLength: 256 | 512 = 256, onlyJs = false): Uint8Array {
const is256 = bitLength === 256;
return isReady() && is256 && !onlyJs
? keccak256(u8aToU8a(value))
: new Uint8Array(
js.keccak256.update(u8aToU8a(value)).arrayBuffer()
(is256 ? js.keccak256 : js.keccak512).update(u8aToU8a(value)).arrayBuffer()
);
}
+1 -1
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
/**
* @summary Create Keccak256 values as hex & Uint8Array output
* @summary Create Keccak256/512 values as hex & Uint8Array output
*/
export { keccakAsHex } from './asHex';