Add toRealUint8Array

This commit is contained in:
Simon Warta 2022-01-24 17:09:28 +01:00
parent 3c7d886eee
commit 8b775c84bb
4 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import { keccak_256 } from "@noble/hashes/sha3";
import { HashFunction } from "./hash";
import { toRealUint8Array } from "./utils";
export class Keccak256 implements HashFunction {
public readonly blockSize = 512 / 8;
@ -14,7 +15,7 @@ export class Keccak256 implements HashFunction {
}
public update(data: Uint8Array): Keccak256 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}

View File

@ -1,6 +1,7 @@
import { ripemd160 as nobleRipemd160 } from "@noble/hashes/ripemd160";
import { HashFunction } from "./hash";
import { toRealUint8Array } from "./utils";
export class Ripemd160 implements HashFunction {
public readonly blockSize = 512 / 8;
@ -14,7 +15,7 @@ export class Ripemd160 implements HashFunction {
}
public update(data: Uint8Array): Ripemd160 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}

View File

@ -2,6 +2,7 @@ import { sha256 as nobleSha256 } from "@noble/hashes/sha256";
import { sha512 as nobleSha512 } from "@noble/hashes/sha512";
import { HashFunction } from "./hash";
import { toRealUint8Array } from "./utils";
export class Sha256 implements HashFunction {
public readonly blockSize = 512 / 8;
@ -15,7 +16,7 @@ export class Sha256 implements HashFunction {
}
public update(data: Uint8Array): Sha256 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}
@ -41,7 +42,7 @@ export class Sha512 implements HashFunction {
}
public update(data: Uint8Array): Sha512 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}

View File

@ -0,0 +1,5 @@
// See https://github.com/paulmillr/noble-hashes/issues/25 for why this is needed
export function toRealUint8Array(data: ArrayLike<number>): Uint8Array {
if (data instanceof Uint8Array) return data;
else return Uint8Array.from(data);
}