Skip to content

Commit 58ae289

Browse files
authored
Merge pull request #43 from artcom/fix-large-data
Fix digesting of large data (more than MAX_UINT32 bits)
2 parents f10eaba + aee24f1 commit 58ae289

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

hash.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ Hash.prototype.digest = function (enc) {
5656
}
5757

5858
// to this append the block which is equal to the number l written in binary
59-
// TODO: handle case where l is > Math.pow(2, 29)
60-
this._block.writeInt32BE(l, this._blockSize - 4)
59+
if (l <= 0xffffffff) {
60+
this._block.writeUInt32BE(l, this._blockSize - 4)
61+
} 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+
}
6166

6267
var hash = this._update(this._block) || this._hash()
6368

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,18 @@ tape('hex encoding', function (t) {
8383

8484
t.end()
8585
})
86+
87+
tape('call digest for more than MAX_UINT32 bits of data', function (t) {
88+
var _hash = crypto.createHash('sha1')
89+
var hash = new Sha1()
90+
var bigData = new Buffer(Math.pow(2, 32) / 8)
91+
92+
hash.update(bigData)
93+
_hash.update(bigData)
94+
95+
var a = hash.digest('hex')
96+
var e = _hash.digest('hex')
97+
98+
t.equal(a, e)
99+
t.end()
100+
})

0 commit comments

Comments
 (0)