Adjust xxhash imports (only 64 used) (#753)

This commit is contained in:
Jaco Greeff
2020-11-08 14:52:28 +01:00
committed by GitHub
parent f431f02f11
commit 1aec77922f
9 changed files with 19 additions and 78 deletions
@@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { xxhash64AsBn } from '.';
import xxhash64AsBn from './asBn';
describe('xxhash64AsBn', (): void => {
it('creates the correct BN output', (): void => {
@@ -5,20 +5,6 @@ import BN from 'bn.js';
import xxhash64AsRaw from './asRaw';
/**
* @name xxhash64AsBn
* @summary Creates a xxhash BN from the input.
* @description
* From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
* @example
* <BR>
*
* ```javascript
* import { xxhash64AsNumber } from '@polkadot/util-crypto';
*
* xxhash64AsBn('abcd', 0xabcd)); // => new BN(0xe29f70f8b8c96df7)
* ```
*/
export default function xxhash64AsBn (data: Buffer | Uint8Array | string, seed: number): BN {
return new BN(
xxhash64AsRaw(data, seed),
@@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { xxhash64AsHex } from '.';
import xxhash64AsHex from './asHex';
describe('xxhash64AsHex', (): void => {
it('creates the correct hex output', (): void => {
@@ -5,20 +5,6 @@ import { hexAddPrefix } from '@polkadot/util';
import xxhash64AsRaw from './asRaw';
/**
* @name xxhash64AsHex
* @summary Creates a xxhash hex from the input.
* @description
* From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
* @example
* <BR>
*
* ```javascript
* import { xxhash64AsHex } from '@polkadot/util-crypto';
*
* xxhash64AsHex('abcd', 0xabcd)); // => 0xe29f70f8b8c96df7
* ```
*/
export default function xxhash64AsHex (data: Buffer | Uint8Array | string, seed: number): string {
return hexAddPrefix(
xxhash64AsRaw(data, seed)
@@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { xxhash64AsRaw } from '.';
import xxhash64AsRaw from './asRaw';
describe('xxhash64AsRaw', (): void => {
it('creates the correct non-prefixed hex output', (): void => {
@@ -3,20 +3,6 @@
import xxhash64AsValue from './asValue';
/**
* @name xxhash64AsRaw
* @summary Creates a xxhash non-prefixed hex from the input.
* @description
* From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a non-prefixed hex string.
* @example
* <BR>
*
* ```javascript
* import { xxhash64AsRaw } from '@polkadot/util-crypto';
*
* xxhash64AsRaw('abcd', 0xabcd)); // => e29f70f8b8c96df7
* ```
*/
export default function xxhash64AsRaw (data: Buffer | Uint8Array | string, seed: number): string {
return xxhash64AsValue(data, seed).toString(16);
}
@@ -1,30 +1,16 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import xxhashjs from 'xxhashjs';
import hasher from 'xxhashjs/lib/xxhash64';
import { isBuffer, isString, u8aToBuffer } from '@polkadot/util';
/**
* @name xxhash64AsValue
* @summary Creates a hex number from the input.
* @description
* From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex number
* @example
* <BR>
*
* ```javascript
* import { xxhash64AsValue } from '@polkadot/util-crypto';
*
* xxhash64AsValue('abcd', 0xabcd)); // => e29f70f8b8c96df7
* ```
*/
export default function xxhash64AsValue (data: Buffer | Uint8Array | string, seed: number): number {
if (isBuffer(data) || isString(data)) {
return xxhashjs.h64(data, seed) as unknown as number;
return hasher(data, seed);
}
return xxhashjs.h64(
return hasher(
u8aToBuffer(data),
seed
) as unknown as number;
);
}
@@ -1,15 +0,0 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import xxhash64AsBn from './asBn';
import xxhash64AsHex from './asHex';
import xxhash64AsRaw from './asRaw';
/**
* @summary Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
*/
export {
xxhash64AsBn,
xxhash64AsHex,
xxhash64AsRaw
};
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
declare module 'xxhashjs/lib/xxhash64' {
export interface HashInterface {
(data: string | Buffer, seed: number): number;
}
const h64: HashInterface;
export = h64;
}