-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathhash_tests.ts
More file actions
323 lines (295 loc) · 9.88 KB
/
hash_tests.ts
File metadata and controls
323 lines (295 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Tests are based on Node.js tests
* https://github.com/nodejs/node/blob/master/test/parallel/test-crypto-hash.js
*/
import {
Buffer,
createHash,
hash,
getHashes,
type Encoding,
} from 'react-native-quick-crypto';
import { expect } from 'chai';
import { test } from '../util';
const SUITE = 'hash';
test(SUITE, 'createHash with valid algorithm', () => {
expect(() => {
createHash('sha256');
}).to.not.throw();
});
test(SUITE, 'createHash with invalid algorithm', () => {
expect(() => {
createHash('sha123');
}).to.throw(/Unknown hash algorithm: sha123/);
});
test(SUITE, 'createHash with null algorithm', () => {
expect(() => {
// @ts-expect-error bad algorithm
createHash(null);
}).to.throw(/Algorithm must be a non-empty string/);
});
test(SUITE, 'check openssl version', () => {
expect(() => {
// Create a hash to trigger OpenSSL initialization
const hash = createHash('sha256');
// Get OpenSSL version directly from the hash object
const version = hash.getOpenSSLVersion();
console.log('OpenSSL Version:', version);
}).to.not.throw();
});
test(SUITE, 'KECCAK-256 using createHash with provider-aware API', () => {
// Test with a simple string
const result1 = createHash('KECCAK-256').update('test').digest();
expect(result1.toString('hex')).to.equal(
'9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658',
);
// Test with empty string
const result2 = createHash('KECCAK-256').update('').digest();
expect(result2.toString('hex')).to.equal(
'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
);
// Test with Buffer
const result3 = createHash('KECCAK-256')
.update(Buffer.from('hello world'))
.digest();
expect(result3.toString('hex')).to.equal(
'47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad',
);
// Verify the result is 32 bytes (256 bits)
expect(result1.length).to.equal(32);
expect(result2.length).to.equal(32);
expect(result3.length).to.equal(32);
// Test that it's different from SHA3-256 (they should be different)
const sha3Hash = createHash('SHA3-256').update('test').digest();
expect(result1.toString('hex')).to.not.equal(sha3Hash.toString('hex'));
});
// test hashing
const a0 = createHash('md5').update('Test123').digest('latin1');
const a1 = createHash('sha1').update('Test123').digest('hex');
const a2 = createHash('sha256').update('Test123').digest('base64');
const a3 = createHash('sha512').update('Test123').digest(); // buffer
const a4 = createHash('sha1').update('Test123').digest('buffer');
test(SUITE, 'non stream - digest with latin1 argument', () => {
expect(a0).to.deep.equal(
'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca\u00bd\u008c',
);
});
test(SUITE, 'non stream - digest with hex argument', () => {
expect(a1).to.deep.equal('8308651804facb7b9af8ffc53a33a22d6a1c8ac2');
});
test(SUITE, 'non stream - digest with base64 argument', () => {
expect(a2).to.deep.equal('2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=');
});
test(SUITE, 'non stream - digest with buffer argument', () => {
expect(a4).to.deep.equal(
Buffer.from('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex'),
);
});
test(SUITE, 'non stream - digest without argument defaults to buffer', () => {
expect(a3).to.deep.equal(
Buffer.from(
"\u00c1(4\u00f1\u0003\u001fd\u0097!O'\u00d4C/&Qz\u00d4" +
'\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
'\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
'\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
"\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed'",
'latin1',
),
);
});
test(SUITE, 'non stream - multiple updates to same hash', () => {
const h1 = createHash('sha1').update('Test').update('123').digest('hex');
expect(h1).to.deep.equal(a1);
});
// stream interface
let a5 = createHash('sha512');
a5.end('Test123');
a5 = a5.read();
let a6 = createHash('sha512');
a6.write('Te');
a6.write('st');
a6.write('123');
a6.end();
a6 = a6.read();
let a7 = createHash('sha512');
a7.end();
a7 = a7.read();
let a8 = createHash('sha512');
a8.write('');
a8.end();
a8 = a8.read();
test(SUITE, 'stream - should produce the same output as non-stream', () => {
expect(a5).to.deep.equal(a3);
expect(a6).to.deep.equal(a3);
});
test(SUITE, 'stream - empty', () => {
expect(a7).to.deep.equal(a8);
expect(a7).not.to.deep.equal(undefined);
expect(a8).not.to.deep.equal(undefined);
});
test(SUITE, 'copy - should create identical hash state', () => {
const hash1 = createHash('sha256').update('Test123');
const hash2 = hash1.copy();
expect(hash1.digest('hex')).to.deep.equal(hash2.digest('hex'));
});
test(SUITE, 'copy - calculate a rolling hash', () => {
const hash = createHash('sha256');
hash.update('one');
expect(hash.copy().digest('hex')).to.deep.equal(
'7692c3ad3540bb803c020b3aee66cd8887123234ea0c6e7143c0add73ff431ed',
);
hash.update('two');
expect(hash.copy().digest('hex')).to.deep.equal(
'25b6746d5172ed6352966a013d93ac846e1110d5a25e8f183b5931f4688842a1',
);
hash.update('three');
expect(hash.copy().digest('hex')).to.deep.equal(
'4592092e1061c7ea85af2aed194621cc17a2762bae33a79bf8ce33fd0168b801',
);
});
test(SUITE, 'getHashes - should return array of supported algorithms', () => {
const algorithms = getHashes();
const expectedAlgorithms = [
'BLAKE2B-512',
'BLAKE2S-256',
'KECCAK-224',
'KECCAK-256',
'KECCAK-384',
'KECCAK-512',
'KECCAK-KMAC-128',
'KECCAK-KMAC-256',
'MD5',
'MD5-SHA1',
'NULL',
'RIPEMD-160',
'SHA1',
'SHA2-224',
'SHA2-256',
'SHA2-256/192',
'SHA2-384',
'SHA2-512',
'SHA2-512/224',
'SHA2-512/256',
'SHA3-224',
'SHA3-256',
'SHA3-384',
'SHA3-512',
'SHAKE-128',
'SHAKE-256',
'SM3',
];
expect(algorithms).to.be.an('array');
expect(algorithms.sort()).to.deep.equal(expectedAlgorithms.sort());
});
// errors
test(SUITE, 'digest - segfault', () => {
const hash = createHash('sha256');
expect(() => {
hash.digest({
toString: () => {
throw new Error('segfault');
},
} as unknown as Encoding);
}).to.throw();
});
test(SUITE, 'update - calling update without argument', () => {
const hash = createHash('sha256');
expect(() => {
// @ts-expect-error calling update without argument
hash.update();
}).to.throw(/Invalid argument type/);
});
test(SUITE, 'digest - calling update after digest', () => {
const hash = createHash('sha256');
hash.digest();
expect(() => hash.update('test')).to.throw(/Failed to update/);
});
// outputLength option
test(SUITE, 'output length = 0', () => {
const hash = createHash('SHAKE-256', { outputLength: 0 });
expect(hash.digest('hex')).to.deep.equal('');
});
test(SUITE, 'output length = 5', () => {
expect(
createHash('shake128', { outputLength: 5 }).digest('hex'),
).to.deep.equal('7f9c2ba4e8');
});
test(SUITE, 'output length with copy', () => {
const hash = createHash('shake128', { outputLength: 5 });
const copy = hash.copy({ outputLength: 0 });
expect(copy.digest('hex')).to.deep.equal('');
expect(hash.digest('hex')).to.deep.equal('7f9c2ba4e8');
});
test(SUITE, 'large output length', () => {
const largeHash = createHash('shake128', { outputLength: 128 }).digest('hex');
expect(largeHash.length).to.equal(2 * 128);
expect(largeHash.slice(0, 32)).to.deep.equal(
'7f9c2ba4e88f827d616045507605853e',
);
expect(largeHash.slice(2 * 128 - 32, 2 * 128)).to.deep.equal(
'df9a04302e10c8bc1cbf1a0b3a5120ea',
);
});
test(SUITE, 'super long hash', () => {
const superLongHash = createHash('shake256', {
outputLength: 1024 * 1024,
})
.update('The message is shorter than the hash!')
.digest('hex');
expect(superLongHash.length).to.equal(2 * 1024 * 1024);
expect(superLongHash.slice(0, 32)).to.deep.equal(
'a2a28dbc49cfd6e5d6ceea3d03e77748',
);
expect(
superLongHash.slice(2 * 1024 * 1024 - 32, 2 * 1024 * 1024),
).to.deep.equal('193414035ddba77bf7bba97981e656ec');
});
test(SUITE, 'unreasonable output length', () => {
expect(() => {
createHash('shake128', { outputLength: 1024 * 1024 * 1024 }).digest('hex');
}).to.throw(
/Output length 1073741824 exceeds maximum allowed size of 16777216/,
);
});
test(SUITE, 'createHash with negative outputLength', () => {
expect(() => {
createHash('shake128', { outputLength: -1 });
}).to.throw(/Output length must be a non-negative number/);
});
test(SUITE, 'createHash with null outputLength', () => {
expect(() => {
// @ts-expect-error bad outputLength
createHash('shake128', { outputLength: null });
}).to.throw(/Output length must be a number/);
});
// crypto.hash() oneshot function tests
test(SUITE, 'hash() oneshot - sha256 hex', () => {
const result = hash('sha256', 'Test123', 'hex');
const expected = createHash('sha256').update('Test123').digest('hex');
expect(result).to.equal(expected);
});
test(SUITE, 'hash() oneshot - sha256 base64', () => {
const result = hash('sha256', 'Test123', 'base64');
const expected = createHash('sha256').update('Test123').digest('base64');
expect(result).to.equal(expected);
});
test(SUITE, 'hash() oneshot - returns Buffer without encoding', () => {
const result = hash('sha256', 'Test123');
expect(Buffer.isBuffer(result)).to.equal(true);
expect(typeof result).to.not.equal('string');
});
test(SUITE, 'hash() oneshot - sha512', () => {
const result = hash('sha512', 'hello world', 'hex');
const expected = createHash('sha512').update('hello world').digest('hex');
expect(result).to.equal(expected);
});
test(SUITE, 'hash() oneshot - md5', () => {
const result = hash('md5', 'Test123', 'hex');
expect(result).to.equal('68eacb97d86f0c4621fa2b0e17cabd8c');
});
test(SUITE, 'hash() oneshot - Buffer input', () => {
const data = Buffer.from('hello');
const result = hash('sha256', data, 'hex');
const expected = createHash('sha256').update(data).digest('hex');
expect(result).to.equal(expected);
});