Skip to content

Commit 666e0a4

Browse files
committed
keep Hash for compatibility
1 parent 1d5713a commit 666e0a4

File tree

4 files changed

+265
-5
lines changed

4 files changed

+265
-5
lines changed

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 } 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';
2+
3+
export function sha256(data: Uint8Array): Uint8Array {
4+
return sha2.sha256(data);
5+
}

src/sha256.ts

Lines changed: 245 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,247 @@
1-
import * as sha2 from '@noble/hashes/sha2';
1+
// SHA-256 for JavaScript.
2+
// Original implementation https://github.com/dchest/fast-sha256-js/blob/master/src/sha256.ts
3+
const digestLength = 32;
4+
const blockSize = 64;
25

3-
export function sha256(data: Uint8Array): Uint8Array {
4-
return sha2.sha256(data);
6+
// SHA-256 constants
7+
const K = new Uint32Array([
8+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
9+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
10+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
11+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
12+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
13+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
14+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
15+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
16+
]);
17+
18+
function hashBlocks(w: Int32Array, v: Int32Array, p: Uint8Array, pos: number, len: number): number {
19+
let a: number,
20+
b: number,
21+
c: number,
22+
d: number,
23+
e: number,
24+
f: number,
25+
g: number,
26+
h: number,
27+
u: number,
28+
i: number,
29+
j: number,
30+
t1: number,
31+
t2: number;
32+
while (len >= 64) {
33+
a = v[0];
34+
b = v[1];
35+
c = v[2];
36+
d = v[3];
37+
e = v[4];
38+
f = v[5];
39+
g = v[6];
40+
h = v[7];
41+
42+
for (i = 0; i < 16; i++) {
43+
j = pos + i * 4;
44+
w[i] =
45+
((p[j] & 0xff) << 24) |
46+
((p[j + 1] & 0xff) << 16) |
47+
((p[j + 2] & 0xff) << 8) |
48+
(p[j + 3] & 0xff);
49+
}
50+
51+
for (i = 16; i < 64; i++) {
52+
u = w[i - 2];
53+
t1 = ((u >>> 17) | (u << (32 - 17))) ^ ((u >>> 19) | (u << (32 - 19))) ^ (u >>> 10);
54+
55+
u = w[i - 15];
56+
t2 = ((u >>> 7) | (u << (32 - 7))) ^ ((u >>> 18) | (u << (32 - 18))) ^ (u >>> 3);
57+
58+
w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
59+
}
60+
61+
for (i = 0; i < 64; i++) {
62+
t1 =
63+
((((((e >>> 6) | (e << (32 - 6))) ^
64+
((e >>> 11) | (e << (32 - 11))) ^
65+
((e >>> 25) | (e << (32 - 25)))) +
66+
((e & f) ^ (~e & g))) |
67+
0) +
68+
((h + ((K[i] + w[i]) | 0)) | 0)) |
69+
0;
70+
71+
t2 =
72+
((((a >>> 2) | (a << (32 - 2))) ^
73+
((a >>> 13) | (a << (32 - 13))) ^
74+
((a >>> 22) | (a << (32 - 22)))) +
75+
((a & b) ^ (a & c) ^ (b & c))) |
76+
0;
77+
78+
h = g;
79+
g = f;
80+
f = e;
81+
e = (d + t1) | 0;
82+
d = c;
83+
c = b;
84+
b = a;
85+
a = (t1 + t2) | 0;
86+
}
87+
88+
v[0] += a;
89+
v[1] += b;
90+
v[2] += c;
91+
v[3] += d;
92+
v[4] += e;
93+
v[5] += f;
94+
v[6] += g;
95+
v[7] += h;
96+
97+
pos += 64;
98+
len -= 64;
99+
}
100+
return pos;
101+
}
102+
103+
/**
104+
* @deprecated Hash implementation of SHA256 has been replaced by noble-hashes SHA256.
105+
*/
106+
export class Hash {
107+
digestLength: number = digestLength;
108+
blockSize: number = blockSize;
109+
110+
// Note: Int32Array is used instead of Uint32Array for performance reasons.
111+
private state: Int32Array = new Int32Array(8); // hash state
112+
private temp: Int32Array = new Int32Array(64); // temporary state
113+
private buffer: Uint8Array = new Uint8Array(128); // buffer for data to hash
114+
private bufferLength = 0; // number of bytes in buffer
115+
private bytesHashed = 0; // number of total bytes hashed
116+
117+
finished = false; // indicates whether the hash was finalized
118+
119+
constructor() {
120+
this.reset();
121+
}
122+
123+
// Resets hash state making it possible
124+
// to re-use this instance to hash other data.
125+
reset(): this {
126+
this.state[0] = 0x6a09e667;
127+
this.state[1] = 0xbb67ae85;
128+
this.state[2] = 0x3c6ef372;
129+
this.state[3] = 0xa54ff53a;
130+
this.state[4] = 0x510e527f;
131+
this.state[5] = 0x9b05688c;
132+
this.state[6] = 0x1f83d9ab;
133+
this.state[7] = 0x5be0cd19;
134+
this.bufferLength = 0;
135+
this.bytesHashed = 0;
136+
this.finished = false;
137+
return this;
138+
}
139+
140+
// Cleans internal buffers and re-initializes hash state.
141+
clean() {
142+
for (let i = 0; i < this.buffer.length; i++) {
143+
this.buffer[i] = 0;
144+
}
145+
for (let i = 0; i < this.temp.length; i++) {
146+
this.temp[i] = 0;
147+
}
148+
this.reset();
149+
}
150+
151+
// Updates hash state with the given data.
152+
//
153+
// Optionally, length of the data can be specified to hash
154+
// fewer bytes than data.length.
155+
//
156+
// Throws error when trying to update already finalized hash:
157+
// instance must be reset to use it again.
158+
update(data: Uint8Array, dataLength: number = data.length): this {
159+
if (this.finished) {
160+
throw new Error("SHA256: can't update because hash was finished.");
161+
}
162+
let dataPos = 0;
163+
this.bytesHashed += dataLength;
164+
if (this.bufferLength > 0) {
165+
while (this.bufferLength < 64 && dataLength > 0) {
166+
this.buffer[this.bufferLength++] = data[dataPos++];
167+
dataLength--;
168+
}
169+
if (this.bufferLength === 64) {
170+
hashBlocks(this.temp, this.state, this.buffer, 0, 64);
171+
this.bufferLength = 0;
172+
}
173+
}
174+
if (dataLength >= 64) {
175+
dataPos = hashBlocks(this.temp, this.state, data, dataPos, dataLength);
176+
dataLength %= 64;
177+
}
178+
while (dataLength > 0) {
179+
this.buffer[this.bufferLength++] = data[dataPos++];
180+
dataLength--;
181+
}
182+
return this;
183+
}
184+
185+
// Finalizes hash state and puts hash into out.
186+
//
187+
// If hash was already finalized, puts the same value.
188+
finish(out: Uint8Array): this {
189+
if (!this.finished) {
190+
const bytesHashed = this.bytesHashed;
191+
const left = this.bufferLength;
192+
const bitLenHi = (bytesHashed / 0x20000000) | 0;
193+
const bitLenLo = bytesHashed << 3;
194+
const padLength = bytesHashed % 64 < 56 ? 64 : 128;
195+
196+
this.buffer[left] = 0x80;
197+
for (let i = left + 1; i < padLength - 8; i++) {
198+
this.buffer[i] = 0;
199+
}
200+
this.buffer[padLength - 8] = (bitLenHi >>> 24) & 0xff;
201+
this.buffer[padLength - 7] = (bitLenHi >>> 16) & 0xff;
202+
this.buffer[padLength - 6] = (bitLenHi >>> 8) & 0xff;
203+
this.buffer[padLength - 5] = (bitLenHi >>> 0) & 0xff;
204+
this.buffer[padLength - 4] = (bitLenLo >>> 24) & 0xff;
205+
this.buffer[padLength - 3] = (bitLenLo >>> 16) & 0xff;
206+
this.buffer[padLength - 2] = (bitLenLo >>> 8) & 0xff;
207+
this.buffer[padLength - 1] = (bitLenLo >>> 0) & 0xff;
208+
209+
hashBlocks(this.temp, this.state, this.buffer, 0, padLength);
210+
211+
this.finished = true;
212+
}
213+
214+
for (let i = 0; i < 8; i++) {
215+
out[i * 4 + 0] = (this.state[i] >>> 24) & 0xff;
216+
out[i * 4 + 1] = (this.state[i] >>> 16) & 0xff;
217+
out[i * 4 + 2] = (this.state[i] >>> 8) & 0xff;
218+
out[i * 4 + 3] = (this.state[i] >>> 0) & 0xff;
219+
}
220+
221+
return this;
222+
}
223+
224+
// Returns the final hash digest.
225+
digest(): Uint8Array {
226+
const out = new Uint8Array(this.digestLength);
227+
this.finish(out);
228+
return out;
229+
}
230+
231+
// Internal function for use in HMAC for optimization.
232+
_saveState(out: Uint32Array) {
233+
for (let i = 0; i < this.state.length; i++) {
234+
out[i] = this.state[i];
235+
}
236+
}
237+
238+
// Internal function for use in HMAC for optimization.
239+
_restoreState(from: Uint32Array, bytesHashed: number) {
240+
for (let i = 0; i < this.state.length; i++) {
241+
this.state[i] = from[i];
242+
}
243+
this.bytesHashed = bytesHashed;
244+
this.finished = false;
245+
this.bufferLength = 0;
246+
}
5247
}

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)