1- var Buffer = require ( 'buffer/ ' ) . Buffer
1+ var Buffer = require ( 'safe- buffer' ) . Buffer
22
33// prototype class for hash functions
44function Hash ( blockSize , finalSize ) {
55 this . _block = new Buffer ( blockSize )
66 this . _finalSize = finalSize
77 this . _blockSize = blockSize
88 this . _len = 0
9- this . _s = 0
109}
1110
1211Hash . prototype . update = function ( data , enc ) {
@@ -15,56 +14,62 @@ Hash.prototype.update = function (data, enc) {
1514 data = new Buffer ( data , enc )
1615 }
1716
18- var l = this . _len += data . length
19- var s = this . _s || 0
20- var f = 0
21- var buffer = this . _block
17+ var block = this . _block
18+ var blockSize = this . _blockSize
19+ var length = data . length
20+ var accum = this . _len
2221
23- while ( s < l ) {
24- var t = Math . min ( data . length , f + this . _blockSize - ( s % this . _blockSize ) )
25- var ch = ( t - f )
22+ for ( var offset = 0 ; offset < length ; ) {
23+ var assigned = accum % blockSize
24+ var remainder = Math . min ( length - offset , blockSize - assigned )
2625
27- for ( var i = 0 ; i < ch ; i ++ ) {
28- buffer [ ( s % this . _blockSize ) + i ] = data [ i + f ]
26+ for ( var i = 0 ; i < remainder ; i ++ ) {
27+ block [ assigned + i ] = data [ offset + i ]
2928 }
3029
31- s += ch
32- f += ch
30+ accum += remainder
31+ offset += remainder
3332
34- if ( ( s % this . _blockSize ) === 0 ) {
35- this . _update ( buffer )
33+ if ( ( accum % blockSize ) === 0 ) {
34+ this . _update ( block )
3635 }
3736 }
38- this . _s = s
3937
38+ this . _len += length
4039 return this
4140}
4241
4342Hash . prototype . digest = function ( enc ) {
44- // Suppose the length of the message M, in bits, is l
45- var l = this . _len * 8
43+ var rem = this . _len % this . _blockSize
4644
47- // Append the bit 1 to the end of the message
48- this . _block [ this . _len % this . _blockSize ] = 0x80
45+ this . _block [ rem ] = 0x80
4946
50- // and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize
51- this . _block . fill ( 0 , this . _len % this . _blockSize + 1 )
47+ // zero (rem + 1) trailing bits, where (rem + 1) is the smallest
48+ // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
49+ this . _block . fill ( 0 , rem + 1 )
5250
53- if ( l % ( this . _blockSize * 8 ) >= this . _finalSize * 8 ) {
51+ if ( rem >= this . _finalSize ) {
5452 this . _update ( this . _block )
5553 this . _block . fill ( 0 )
5654 }
5755
58- // to this append the block which is equal to the number l written in binary
59- if ( l <= 0xffffffff ) {
60- this . _block . writeUInt32BE ( l , this . _blockSize - 4 )
56+ var bits = this . _len * 8
57+
58+ // uint32
59+ if ( bits <= 0xffffffff ) {
60+ this . _block . writeUInt32BE ( bits , this . _blockSize - 4 )
61+
62+ // uint64
6163 } else {
62- var ll = l & 0xffffffff
63- this . _block . writeUInt32BE ( ( l - ll ) / 0x100000000 , this . _blockSize - 8 )
64- this . _block . writeUInt32BE ( ll , this . _blockSize - 4 )
64+ var lowBits = bits & 0xffffffff
65+ var highBits = ( bits - lowBits ) / 0x100000000
66+
67+ this . _block . writeUInt32BE ( highBits , this . _blockSize - 8 )
68+ this . _block . writeUInt32BE ( lowBits , this . _blockSize - 4 )
6569 }
6670
67- var hash = this . _update ( this . _block ) || this . _hash ( )
71+ this . _update ( this . _block )
72+ var hash = this . _hash ( )
6873
6974 return enc ? hash . toString ( enc ) : hash
7075}
0 commit comments