User ArrayBuffer internally to xxhash (#1133)

This commit is contained in:
Jaco
2021-10-23 11:30:23 +03:00
committed by GitHub
parent 15e85bf4b9
commit c7c1723914
4 changed files with 15 additions and 11 deletions
+5 -4
View File
@@ -24,16 +24,17 @@ import xxhash64AsBn from './xxhash64/asBn';
*/
export function xxhashAsU8a (data: HexString | Buffer | Uint8Array | string, bitLength = 64, onlyJs = false): Uint8Array {
const iterations = Math.ceil(bitLength / 64);
const u8a = u8aToU8a(data);
if (isReady() && !onlyJs) {
return twox(u8aToU8a(data), iterations);
return twox(u8a, iterations);
}
const u8a = new Uint8Array(Math.ceil(bitLength / 8));
const result = new Uint8Array(Math.ceil(bitLength / 8));
for (let seed = 0; seed < iterations; seed++) {
u8a.set(xxhash64AsBn(data, seed).toArray('le', 8), seed * 8);
result.set(xxhash64AsBn(u8a, seed).toArray('le', 8), seed * 8);
}
return u8a;
return result;
}
@@ -3,15 +3,11 @@
import xx from 'xxhashjs';
import { isBuffer, isString, u8aToBuffer } from '@polkadot/util';
import { u8aToU8a } from '@polkadot/util';
export default function xxhash64AsValue (data: Buffer | Uint8Array | string, seed: number): number {
if (isBuffer(data) || isString(data)) {
return xx.h64(data, seed);
}
return xx.h64(
u8aToBuffer(data),
u8aToU8a(data).buffer,
seed
);
}
+1 -1
View File
@@ -3,7 +3,7 @@
declare module 'xxhashjs' {
export interface HashInterface {
(data: string | Buffer, seed: number): number;
(data: string | Buffer | ArrayBuffer, seed: number): number;
}
const h64: HashInterface;