11'use strict' ;
22
33var 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
107function Hash ( blockSize , finalSize ) {
@@ -22,31 +19,9 @@ Hash.prototype.update = function (data, enc) {
2219 * Already single-byte wide and 0-255
2320 */
2421 } else if ( typeof data === 'string' ) {
25- enc = enc || 'utf8' ;
26- 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- }
22+ data = Buffer . from ( data , enc || 'utf8' )
3523 } 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- }
24+ data = toBuffer ( data , enc || 'utf8' )
5025 }
5126
5227 var block = this . _block ;
0 commit comments