Skip to content

Commit f2a258e

Browse files
ChALkeRljharb
authored andcommitted
[Fix] support multi-byte wide typed arrays
1 parent 00c7f23 commit f2a258e

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

.eslintrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,17 @@
6060
"max-lines-per-function": "off",
6161
},
6262
},
63+
{
64+
"files": "hash.js",
65+
"globals": {
66+
"Uint8Array": false,
67+
},
68+
},
69+
{
70+
"files": "test/test.js",
71+
"globals": {
72+
"Uint16Array": false,
73+
},
74+
},
6375
],
6476
}

hash.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var Buffer = require('safe-buffer').Buffer;
4+
var toBuffer = require('to-buffer');
45

56
// prototype class for hash functions
67
function Hash(blockSize, finalSize) {
@@ -12,10 +13,7 @@ function Hash(blockSize, finalSize) {
1213

1314
Hash.prototype.update = function (data, enc) {
1415
/* eslint no-param-reassign: 0 */
15-
if (typeof data === 'string') {
16-
enc = enc || 'utf8';
17-
data = Buffer.from(data, enc);
18-
}
16+
data = toBuffer(data, enc || 'utf8');
1917

2018
var block = this._block;
2119
var blockSize = this._blockSize;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"dependencies": {
1111
"inherits": "^2.0.4",
12-
"safe-buffer": "^5.2.1"
12+
"safe-buffer": "^5.2.1",
13+
"to-buffer": "^1.2.0"
1314
},
1415
"devDependencies": {
1516
"@ljharb/eslint-config": "^21.1.1",

test/test.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ var Buffer = require('safe-buffer').Buffer;
66

77
var Sha1 = require('../').sha1;
88

9+
var nodeSupportsUint16 = false;
10+
try {
11+
crypto.createHash('sha1').update(new Uint16Array());
12+
nodeSupportsUint16 = true;
13+
} catch (err) {}
14+
915
var inputs = [
1016
['', 'ascii'],
1117
['abc', 'ascii'],
@@ -15,8 +21,10 @@ var inputs = [
1521
['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
1622
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
1723
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
18-
['foobarbaz', 'ascii']
19-
];
24+
['foobarbaz', 'ascii'],
25+
[Buffer.from('buffer')],
26+
nodeSupportsUint16 ? [new Uint16Array([1, 2, 3])] : null
27+
].filter(Boolean);
2028

2129
tape("hash is the same as node's crypto", function (t) {
2230
inputs.forEach(function (v) {
@@ -35,7 +43,7 @@ tape('call update multiple times', function (t) {
3543
var sha1hash = crypto.createHash('sha1');
3644

3745
for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
38-
var s = v[0].substring(i, (i + 1) * 2);
46+
var s = v[0].slice(i, (i + 1) * 2);
3947
hash.update(s, v[1]);
4048
sha1hash.update(s, v[1]);
4149
}
@@ -74,7 +82,7 @@ tape('hex encoding', function (t) {
7482
var sha1hash = crypto.createHash('sha1');
7583

7684
for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
77-
var s = v[0].substring(i, (i + 1) * 2);
85+
var s = v[0].slice(i, (i + 1) * 2);
7886
hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex');
7987
sha1hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex');
8088
}
@@ -88,6 +96,29 @@ tape('hex encoding', function (t) {
8896
t.end();
8997
});
9098

99+
tape('throws on invalid input', function (t) {
100+
var invalid = [
101+
{}, // non-arrayish
102+
{ length: 20 }, // undefined values
103+
[NaN], // non-numbers
104+
[[]], // non-numbers
105+
[1, 1.5], // non-integers
106+
[1, 256], // out of bounds
107+
[-1, 0] // out of bounds
108+
];
109+
110+
invalid.forEach(function (input) {
111+
var hash = new Sha1();
112+
113+
t['throws'](function () {
114+
hash.update(input);
115+
hash.digest('hex');
116+
});
117+
});
118+
119+
t.end();
120+
});
121+
91122
tape('call digest for more than MAX_UINT32 bits of data', function (t) {
92123
var sha1hash = crypto.createHash('sha1');
93124
var hash = new Sha1();

0 commit comments

Comments
 (0)