1
- var Buffer = require ( 'buffer/ ' ) . Buffer
1
+ var Buffer = require ( 'safe- buffer' ) . Buffer
2
2
3
3
// prototype class for hash functions
4
4
function Hash ( blockSize , finalSize ) {
5
5
this . _block = new Buffer ( blockSize )
6
6
this . _finalSize = finalSize
7
7
this . _blockSize = blockSize
8
8
this . _len = 0
9
- this . _s = 0
10
9
}
11
10
12
11
Hash . prototype . update = function ( data , enc ) {
@@ -15,56 +14,62 @@ Hash.prototype.update = function (data, enc) {
15
14
data = new Buffer ( data , enc )
16
15
}
17
16
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
22
21
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 )
26
25
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 ]
29
28
}
30
29
31
- s += ch
32
- f += ch
30
+ accum += remainder
31
+ offset += remainder
33
32
34
- if ( ( s % this . _blockSize ) === 0 ) {
35
- this . _update ( buffer )
33
+ if ( ( accum % blockSize ) === 0 ) {
34
+ this . _update ( block )
36
35
}
37
36
}
38
- this . _s = s
39
37
38
+ this . _len += length
40
39
return this
41
40
}
42
41
43
42
Hash . 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
46
44
47
- // Append the bit 1 to the end of the message
48
- this . _block [ this . _len % this . _blockSize ] = 0x80
45
+ this . _block [ rem ] = 0x80
49
46
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 )
52
50
53
- if ( l % ( this . _blockSize * 8 ) >= this . _finalSize * 8 ) {
51
+ if ( rem >= this . _finalSize ) {
54
52
this . _update ( this . _block )
55
53
this . _block . fill ( 0 )
56
54
}
57
55
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
61
63
} 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 )
65
69
}
66
70
67
- var hash = this . _update ( this . _block ) || this . _hash ( )
71
+ this . _update ( this . _block )
72
+ var hash = this . _hash ( )
68
73
69
74
return enc ? hash . toString ( enc ) : hash
70
75
}
0 commit comments