Skip to content

Commit 51923de

Browse files
authored
Merge pull request #27 from iden3/PID-3513-switch-sha256-noble-hashes
Switch to noble-hashes
2 parents 336e052 + d782e5c commit 51923de

10 files changed

Lines changed: 36 additions & 23 deletions

File tree

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v20.11.1
1+
v20.19.0

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@iden3/js-crypto",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Crypto primitives for iden3",
55
"source": "./src/index.ts",
66
"exports": {
@@ -54,6 +54,6 @@
5454
"vitest": "^3.1.3"
5555
},
5656
"dependencies": {
57-
"@noble/hashes": "^1.8.0"
57+
"@noble/hashes": "^2.0.1"
5858
}
5959
}

src/babyjub/eddsa-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { babyJub } from './babyjub';
22
import { Eddsa, eddsa } from './eddsa';
33
import { Hex } from '../hex';
4-
import { blake512 } from '@noble/hashes/blake1';
4+
import { blake512 } from '@noble/hashes/blake1.js';
55
import { utils } from '../ff';
66

77
export class Signature {

src/babyjub/eddsa.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { babyJub, BabyJub } from './babyjub';
22
import { poseidon } from '../poseidon';
33
import { F1Field, Scalar, utils } from '../ff';
44
import { PublicKey, Signature } from './eddsa-keys';
5-
import { blake512 } from '@noble/hashes/blake1';
5+
import { blake512 } from '@noble/hashes/blake1.js';
66

77
export class Eddsa {
88
babyJub: BabyJub = babyJub;

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export { Eddsa, eddsa, BabyJub, babyJub, Signature, PublicKey, PrivateKey } from
22
export { poseidon, Poseidon, OPT } from './poseidon';
33
export { Hex } from './hex';
44
export { base58FromBytes, base58ToBytes } from './base58';
5-
export { sha256, Hash } from './sha256';
5+
export { Hash } from './sha256';
6+
export { sha256 } from './sha256-noble';
67
export { utils as ffUtils, getRandomBytes } from './ff';

src/sha256-noble.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as sha2 from '@noble/hashes/sha2.js';
2+
3+
export function sha256(data: Uint8Array): Uint8Array {
4+
return sha2.sha256(data);
5+
}

src/sha256.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ function hashBlocks(w: Int32Array, v: Int32Array, p: Uint8Array, pos: number, le
100100
return pos;
101101
}
102102

103-
// Hash implements SHA256 hash algorithm.
103+
/**
104+
* @deprecated Hash implementation of SHA256 has been replaced by noble-hashes SHA256.
105+
*/
104106
export class Hash {
105107
digestLength: number = digestLength;
106108
blockSize: number = blockSize;
@@ -243,10 +245,3 @@ export class Hash {
243245
this.bufferLength = 0;
244246
}
245247
}
246-
247-
export function sha256(data: Uint8Array): Uint8Array {
248-
const h = new Hash().update(data);
249-
const digest = h.digest();
250-
h.clean();
251-
return digest;
252-
}

tests/blake512.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Hex } from '../src';
2-
import { blake512 } from '@noble/hashes/blake1';
2+
import { blake512 } from '@noble/hashes/blake1.js';
33

44
describe('blake512 hash', () => {
55
it('blake512', () => {

tests/sha256.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { sha256 } from '../src/sha256';
1+
import { sha256 } from '../src/sha256-noble';
2+
import { Hash } from '../src/sha256';
23
import { Hex } from '../src/hex';
34

45
describe('SHA-256 Hashing', () => {
56
const encoder = new TextEncoder();
7+
8+
function sha256Old(data: Uint8Array): Uint8Array {
9+
const h = new Hash().update(data);
10+
const digest = h.digest();
11+
h.clean();
12+
return digest;
13+
}
14+
615
it('should correctly hash a string', () => {
716
const suite = [
817
{
@@ -25,6 +34,9 @@ describe('SHA-256 Hashing', () => {
2534
for (const { input, expectedHash } of suite) {
2635
const result = Hex.encodeString(sha256(encoder.encode(input)));
2736
expect(result).toEqual(expectedHash);
37+
38+
const resultOld = Hex.encodeString(sha256Old(encoder.encode(input)));
39+
expect(resultOld).toEqual(expectedHash);
2840
}
2941
});
3042

0 commit comments

Comments
 (0)