Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ripemd160 dependencies with @noble/hashes #960

Merged
merged 4 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 14 additions & 46 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .yarn/cache/@noble-hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip
Git LFS file not shown

This file was deleted.

3 changes: 0 additions & 3 deletions .yarn/cache/@types-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip

This file was deleted.

3 changes: 0 additions & 3 deletions .yarn/cache/js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip

This file was deleted.

8 changes: 2 additions & 6 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@
"@cosmjs/encoding": "workspace:packages/encoding",
"@cosmjs/math": "workspace:packages/math",
"@cosmjs/utils": "workspace:packages/utils",
"@noble/hashes": "^1",
"bip39": "^3.0.2",
"bn.js": "^5.2.0",
"elliptic": "^6.5.3",
"js-sha3": "^0.8.0",
"libsodium-wrappers": "^0.7.6",
"ripemd160": "^2.0.2",
"sha.js": "^2.4.11"
"libsodium-wrappers": "^0.7.6"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
Expand All @@ -63,8 +61,6 @@
"@types/karma-jasmine-html-reporter": "^1",
"@types/libsodium-wrappers": "^0.7.7",
"@types/node": "^15.0.1",
"@types/ripemd160": "^2.0.0",
"@types/sha.js": "^2.4.0",
"@typescript-eslint/eslint-plugin": "^4.28",
"@typescript-eslint/parser": "^4.28",
"buffer": "^6.0.3",
Expand Down
11 changes: 5 additions & 6 deletions packages/crypto/src/keccak.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import jssha3 from "js-sha3";
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;

private readonly impl: jssha3.Hasher;
private readonly impl = keccak_256.create();

public constructor(firstData?: Uint8Array) {
this.impl = jssha3.keccak256.create();

if (firstData) {
this.update(firstData);
}
}

public update(data: Uint8Array): Keccak256 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}

public digest(): Uint8Array {
return new Uint8Array(this.impl.digest());
return this.impl.digest();
}
}

Expand Down
9 changes: 5 additions & 4 deletions packages/crypto/src/ripemd.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import RIPEMD160 from "ripemd160";
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;

private readonly impl = new RIPEMD160();
private readonly impl = nobleRipemd160.create();

public constructor(firstData?: Uint8Array) {
if (firstData) {
Expand All @@ -14,12 +15,12 @@ export class Ripemd160 implements HashFunction {
}

public update(data: Uint8Array): Ripemd160 {
this.impl.update(Buffer.from(data));
this.impl.update(toRealUint8Array(data));
return this;
}

public digest(): Uint8Array {
return Uint8Array.from(this.impl.digest());
return this.impl.digest();
}
}

Expand Down
21 changes: 9 additions & 12 deletions packages/crypto/src/sha.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { Hash } from "crypto";
import shajs from "sha.js";
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;

private readonly impl: Hash;
private readonly impl = nobleSha256.create();

public constructor(firstData?: Uint8Array) {
this.impl = shajs("sha256");

if (firstData) {
this.update(firstData);
}
}

public update(data: Uint8Array): Sha256 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}

public digest(): Uint8Array {
return new Uint8Array(this.impl.digest());
return this.impl.digest();
}
}

Expand All @@ -34,23 +33,21 @@ export function sha256(data: Uint8Array): Uint8Array {
export class Sha512 implements HashFunction {
public readonly blockSize = 1024 / 8;

private readonly impl: Hash;
private readonly impl = nobleSha512.create();

public constructor(firstData?: Uint8Array) {
this.impl = shajs("sha512");

if (firstData) {
this.update(firstData);
}
}

public update(data: Uint8Array): Sha512 {
this.impl.update(data);
this.impl.update(toRealUint8Array(data));
return this;
}

public digest(): Uint8Array {
return new Uint8Array(this.impl.digest());
return this.impl.digest();
}
}

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);
}
Loading