@@ -41,27 +41,32 @@ Hash.prototype.update = function (data, enc) {
41
41
}
42
42
43
43
Hash . prototype . digest = function ( enc ) {
44
- // Suppose the length of the message M, in bits, is l
45
- var l = this . _len * 8
44
+ var rem = this . _len % this . _blockSize
46
45
47
- // Append the bit 1 to the end of the message
48
- this . _block [ this . _len % this . _blockSize ] = 0x80
46
+ this . _block [ rem ] = 0x80
49
47
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 )
48
+ // zero (rem + 1) trailing bits, where (rem + 1) is the smallest
49
+ // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
50
+ this . _block . fill ( 0 , rem + 1 )
52
51
53
- if ( l % ( this . _blockSize * 8 ) >= this . _finalSize * 8 ) {
52
+ if ( rem >= this . _finalSize ) {
54
53
this . _update ( this . _block )
55
54
this . _block . fill ( 0 )
56
55
}
57
56
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 )
57
+ var bits = this . _len * 8
58
+
59
+ // uint32
60
+ if ( bits <= 0xffffffff ) {
61
+ this . _block . writeUInt32BE ( bits , this . _blockSize - 4 )
62
+
63
+ // uint64
61
64
} else {
62
- var ll = l & 0xffffffff
63
- this . _block . writeUInt32BE ( ( l - ll ) / 0x100000000 , this . _blockSize - 8 )
64
- this . _block . writeUInt32BE ( ll , this . _blockSize - 4 )
65
+ var lowBits = bits & 0xffffffff
66
+ var highBits = ( bits - lowBits ) / 0x100000000
67
+
68
+ this . _block . writeUInt32BE ( highBits , this . _blockSize - 8 )
69
+ this . _block . writeUInt32BE ( lowBits , this . _blockSize - 4 )
65
70
}
66
71
67
72
this . _update ( this . _block )
0 commit comments