From 8b775c84bb57580468bc7d7a618dcfeaaa0a8f29 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 24 Jan 2022 17:09:28 +0100 Subject: [PATCH] Add toRealUint8Array --- packages/crypto/src/keccak.ts | 3 ++- packages/crypto/src/ripemd.ts | 3 ++- packages/crypto/src/sha.ts | 5 +++-- packages/crypto/src/utils.ts | 5 +++++ 4 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 packages/crypto/src/utils.ts diff --git a/packages/crypto/src/keccak.ts b/packages/crypto/src/keccak.ts index 66f647a788..4f82b047ef 100644 --- a/packages/crypto/src/keccak.ts +++ b/packages/crypto/src/keccak.ts @@ -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; } diff --git a/packages/crypto/src/ripemd.ts b/packages/crypto/src/ripemd.ts index 263e573c09..2aad9402f5 100644 --- a/packages/crypto/src/ripemd.ts +++ b/packages/crypto/src/ripemd.ts @@ -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; } diff --git a/packages/crypto/src/sha.ts b/packages/crypto/src/sha.ts index 25656aed11..4e59e81d73 100644 --- a/packages/crypto/src/sha.ts +++ b/packages/crypto/src/sha.ts @@ -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; } diff --git a/packages/crypto/src/utils.ts b/packages/crypto/src/utils.ts new file mode 100644 index 0000000000..9dbec0d0e0 --- /dev/null +++ b/packages/crypto/src/utils.ts @@ -0,0 +1,5 @@ +// See https://github.com/paulmillr/noble-hashes/issues/25 for why this is needed +export function toRealUint8Array(data: ArrayLike): Uint8Array { + if (data instanceof Uint8Array) return data; + else return Uint8Array.from(data); +}