diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
commit | 0469abd4a9c9270a1fdc962969e36e63699af8b4 (patch) | |
tree | f9864d4a4148621378958794cbbfdc2393733283 /node_modules/sha.js/hash.js | |
parent | 6947e79bbc258f7bc96af424ddb71a511f0c15a3 (diff) |
upgrade dependencies
Diffstat (limited to 'node_modules/sha.js/hash.js')
-rw-r--r-- | node_modules/sha.js/hash.js | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/node_modules/sha.js/hash.js b/node_modules/sha.js/hash.js index 09579d2de..f7cbd0714 100644 --- a/node_modules/sha.js/hash.js +++ b/node_modules/sha.js/hash.js @@ -1,63 +1,75 @@ +var Buffer = require('safe-buffer').Buffer + // prototype class for hash functions function Hash (blockSize, finalSize) { - this._block = new Buffer(blockSize) + this._block = Buffer.alloc(blockSize) this._finalSize = finalSize this._blockSize = blockSize this._len = 0 - this._s = 0 } Hash.prototype.update = function (data, enc) { if (typeof data === 'string') { enc = enc || 'utf8' - data = new Buffer(data, enc) + data = Buffer.from(data, enc) } - var l = this._len += data.length - var s = this._s || 0 - var f = 0 - var buffer = this._block + var block = this._block + var blockSize = this._blockSize + var length = data.length + var accum = this._len - while (s < l) { - var t = Math.min(data.length, f + this._blockSize - (s % this._blockSize)) - var ch = (t - f) + for (var offset = 0; offset < length;) { + var assigned = accum % blockSize + var remainder = Math.min(length - offset, blockSize - assigned) - for (var i = 0; i < ch; i++) { - buffer[(s % this._blockSize) + i] = data[i + f] + for (var i = 0; i < remainder; i++) { + block[assigned + i] = data[offset + i] } - s += ch - f += ch + accum += remainder + offset += remainder - if ((s % this._blockSize) === 0) { - this._update(buffer) + if ((accum % blockSize) === 0) { + this._update(block) } } - this._s = s + this._len += length return this } Hash.prototype.digest = function (enc) { - // Suppose the length of the message M, in bits, is l - var l = this._len * 8 + var rem = this._len % this._blockSize - // Append the bit 1 to the end of the message - this._block[this._len % this._blockSize] = 0x80 + this._block[rem] = 0x80 - // and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize - this._block.fill(0, this._len % this._blockSize + 1) + // zero (rem + 1) trailing bits, where (rem + 1) is the smallest + // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize + this._block.fill(0, rem + 1) - if (l % (this._blockSize * 8) >= this._finalSize * 8) { + if (rem >= this._finalSize) { this._update(this._block) this._block.fill(0) } - // to this append the block which is equal to the number l written in binary - // TODO: handle case where l is > Math.pow(2, 29) - this._block.writeInt32BE(l, this._blockSize - 4) + var bits = this._len * 8 + + // uint32 + if (bits <= 0xffffffff) { + this._block.writeUInt32BE(bits, this._blockSize - 4) + + // uint64 + } else { + var lowBits = bits & 0xffffffff + var highBits = (bits - lowBits) / 0x100000000 + + this._block.writeUInt32BE(highBits, this._blockSize - 8) + this._block.writeUInt32BE(lowBits, this._blockSize - 4) + } - var hash = this._update(this._block) || this._hash() + this._update(this._block) + var hash = this._hash() return enc ? hash.toString(enc) : hash } |