2017-12-10 21:51:33 +01:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
// prototype class for hash functions
|
|
|
|
function Hash (blockSize, finalSize) {
|
2017-12-10 21:51:33 +01:00
|
|
|
this._block = Buffer.alloc(blockSize)
|
2017-05-03 15:35:00 +02:00
|
|
|
this._finalSize = finalSize
|
|
|
|
this._blockSize = blockSize
|
|
|
|
this._len = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash.prototype.update = function (data, enc) {
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
enc = enc || 'utf8'
|
2017-12-10 21:51:33 +01:00
|
|
|
data = Buffer.from(data, enc)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var block = this._block
|
|
|
|
var blockSize = this._blockSize
|
|
|
|
var length = data.length
|
|
|
|
var accum = this._len
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
for (var offset = 0; offset < length;) {
|
|
|
|
var assigned = accum % blockSize
|
|
|
|
var remainder = Math.min(length - offset, blockSize - assigned)
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
for (var i = 0; i < remainder; i++) {
|
|
|
|
block[assigned + i] = data[offset + i]
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
accum += remainder
|
|
|
|
offset += remainder
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if ((accum % blockSize) === 0) {
|
|
|
|
this._update(block)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
this._len += length
|
2017-05-03 15:35:00 +02:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash.prototype.digest = function (enc) {
|
2017-12-10 21:51:33 +01:00
|
|
|
var rem = this._len % this._blockSize
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
this._block[rem] = 0x80
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// 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)
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if (rem >= this._finalSize) {
|
2017-05-03 15:35:00 +02:00
|
|
|
this._update(this._block)
|
|
|
|
this._block.fill(0)
|
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
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)
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
this._update(this._block)
|
|
|
|
var hash = this._hash()
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
return enc ? hash.toString(enc) : hash
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash.prototype._update = function () {
|
|
|
|
throw new Error('_update must be implemented by subclass')
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Hash
|