diff options
Diffstat (limited to 'node_modules/hash-base/index.js')
-rw-r--r-- | node_modules/hash-base/index.js | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/node_modules/hash-base/index.js b/node_modules/hash-base/index.js index 9cd6a7005..bf788daa4 100644 --- a/node_modules/hash-base/index.js +++ b/node_modules/hash-base/index.js @@ -1,11 +1,18 @@ 'use strict' +var Buffer = require('safe-buffer').Buffer var Transform = require('stream').Transform var inherits = require('inherits') +function throwIfNotStringOrBuffer (val, prefix) { + if (!Buffer.isBuffer(val) && typeof val !== 'string') { + throw new TypeError(prefix + ' must be a string or a buffer') + } +} + function HashBase (blockSize) { Transform.call(this) - this._block = new Buffer(blockSize) + this._block = Buffer.allocUnsafe(blockSize) this._blockSize = blockSize this._blockOffset = 0 this._length = [0, 0, 0, 0] @@ -18,8 +25,7 @@ inherits(HashBase, Transform) HashBase.prototype._transform = function (chunk, encoding, callback) { var error = null try { - if (encoding !== 'buffer') chunk = new Buffer(chunk, encoding) - this.update(chunk) + this.update(chunk, encoding) } catch (err) { error = err } @@ -30,7 +36,7 @@ HashBase.prototype._transform = function (chunk, encoding, callback) { HashBase.prototype._flush = function (callback) { var error = null try { - this.push(this._digest()) + this.push(this.digest()) } catch (err) { error = err } @@ -39,9 +45,9 @@ HashBase.prototype._flush = function (callback) { } HashBase.prototype.update = function (data, encoding) { - if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer') + throwIfNotStringOrBuffer(data, 'Data') if (this._finalized) throw new Error('Digest already called') - if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding || 'binary') + if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding) // consume data var block = this._block @@ -63,7 +69,7 @@ HashBase.prototype.update = function (data, encoding) { return this } -HashBase.prototype._update = function (data) { +HashBase.prototype._update = function () { throw new Error('_update is not implemented') } @@ -73,6 +79,12 @@ HashBase.prototype.digest = function (encoding) { var digest = this._digest() if (encoding !== undefined) digest = digest.toString(encoding) + + // reset state + this._block.fill(0) + this._blockOffset = 0 + for (var i = 0; i < 4; ++i) this._length[i] = 0 + return digest } |