aboutsummaryrefslogtreecommitdiff
path: root/node_modules/hash-base/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/hash-base/index.js')
-rw-r--r--node_modules/hash-base/index.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/node_modules/hash-base/index.js b/node_modules/hash-base/index.js
new file mode 100644
index 000000000..9cd6a7005
--- /dev/null
+++ b/node_modules/hash-base/index.js
@@ -0,0 +1,83 @@
+'use strict'
+var Transform = require('stream').Transform
+var inherits = require('inherits')
+
+function HashBase (blockSize) {
+ Transform.call(this)
+
+ this._block = new Buffer(blockSize)
+ this._blockSize = blockSize
+ this._blockOffset = 0
+ this._length = [0, 0, 0, 0]
+
+ this._finalized = false
+}
+
+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)
+ } catch (err) {
+ error = err
+ }
+
+ callback(error)
+}
+
+HashBase.prototype._flush = function (callback) {
+ var error = null
+ try {
+ this.push(this._digest())
+ } catch (err) {
+ error = err
+ }
+
+ callback(error)
+}
+
+HashBase.prototype.update = function (data, encoding) {
+ if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
+ if (this._finalized) throw new Error('Digest already called')
+ if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding || 'binary')
+
+ // consume data
+ var block = this._block
+ var offset = 0
+ while (this._blockOffset + data.length - offset >= this._blockSize) {
+ for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++]
+ this._update()
+ this._blockOffset = 0
+ }
+ while (offset < data.length) block[this._blockOffset++] = data[offset++]
+
+ // update length
+ for (var j = 0, carry = data.length * 8; carry > 0; ++j) {
+ this._length[j] += carry
+ carry = (this._length[j] / 0x0100000000) | 0
+ if (carry > 0) this._length[j] -= 0x0100000000 * carry
+ }
+
+ return this
+}
+
+HashBase.prototype._update = function (data) {
+ throw new Error('_update is not implemented')
+}
+
+HashBase.prototype.digest = function (encoding) {
+ if (this._finalized) throw new Error('Digest already called')
+ this._finalized = true
+
+ var digest = this._digest()
+ if (encoding !== undefined) digest = digest.toString(encoding)
+ return digest
+}
+
+HashBase.prototype._digest = function () {
+ throw new Error('_digest is not implemented')
+}
+
+module.exports = HashBase