Skip to content

Commit

Permalink
Add toRealUint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Jan 26, 2022
1 parent 3c7d886 commit 8b775c8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/crypto/src/keccak.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/crypto/src/ripemd.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/crypto/src/sha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/crypto/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8b775c8

Please sign in to comment.