Skip to content

Commit c0f425f

Browse files
committed
use to-buffer
1 parent f76e123 commit c0f425f

File tree

2 files changed

+5
-27
lines changed

2 files changed

+5
-27
lines changed

hash.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
'use strict';
22

33
var Buffer = require('safe-buffer').Buffer;
4-
5-
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
6-
&& typeof Uint8Array !== 'undefined'
7-
&& ArrayBuffer.isView;
4+
var toBuffer = require('to-buffer')
85

96
// prototype class for hash functions
107
function Hash(blockSize, finalSize) {
@@ -24,29 +21,9 @@ Hash.prototype.update = function (data, enc) {
2421
} else if (typeof data === 'string') {
2522
enc = enc || 'utf8';
2623
data = Buffer.from(data, enc);
27-
} else if (useArrayBuffer && ArrayBuffer.isView(data)) {
28-
// Convert all TypedArray and DataView instances to single-byte-wide Uint8Array views
29-
var oldSize = data.byteLength;
30-
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
31-
32-
if (!(data.byteLength === oldSize && data.byteLength === data.length)) {
33-
throw new Error('Unexpected: broken Uint8Array');
34-
}
3524
} else {
36-
if (!data || typeof data !== 'object' || typeof data.length !== 'number') {
37-
throw new TypeError('Not an array-like');
38-
}
39-
40-
// non-negative 32-bit integer
41-
if ((data.length >>> 0) !== data.length) {
42-
throw new RangeError('Invalid length');
43-
}
44-
45-
for (var j = 0; j < data.length; j++) {
46-
if ((data[j] & 255) !== data[j]) {
47-
throw new TypeError('Not a byte array');
48-
}
49-
}
25+
enc = enc || 'utf8';
26+
data = toBuffer(data, enc);
5027
}
5128

5229
var block = this._block;

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",

0 commit comments

Comments
 (0)