Comparing version 1.0.2 to 2.0.0
46
index.js
@@ -5,5 +5,11 @@ 'use strict' | ||
function HashBase () { | ||
function HashBase (blockSize) { | ||
Transform.call(this) | ||
this.initialised_ = true | ||
this._block = new Buffer(blockSize) | ||
this._blockSize = blockSize | ||
this._blockOffset = 0 | ||
this._length = [0, 0, 0, 0] | ||
this._finalized = false | ||
} | ||
@@ -13,12 +19,25 @@ | ||
HashBase.DEFAULT_ENCODING = 'buffer' | ||
HashBase.prototype.update = function (data, encoding) { | ||
if (this._finalized) throw new Error('Digest already called') | ||
if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding || 'binary') | ||
HashBase.prototype.update = function (data, encoding) { | ||
if (!Buffer.isBuffer(data)) { | ||
if (encoding === undefined) encoding = HashBase.DEFAULT_ENCODING | ||
if (encoding === 'buffer') encoding = 'binary' | ||
data = new Buffer(data, encoding) | ||
// consume data | ||
var offset = 0 | ||
while (this._blockOffset + data.length - offset >= this._blockSize) { | ||
for (var i = this._blockOffset; i < this._blockSize;) this._block[i++] = data[offset++] | ||
this._update() | ||
this._blockOffset = 0 | ||
} | ||
this._update(data) | ||
while (offset < data.length) { | ||
this._block[this._blockOffset++] = data[offset++] | ||
} | ||
// update length | ||
for (var j = 0, carry = data.length * 8; carry > 0; ++j) { | ||
this._length[j] += carry | ||
carry = Math.floor(this._length[j] / 0x0100000000) | ||
if (carry > 0) this._length[j] -= 0x0100000000 * carry | ||
} | ||
return this | ||
@@ -28,8 +47,7 @@ } | ||
HashBase.prototype.digest = function (encoding) { | ||
if (!this.initialised_) throw new Error('Not initialized') | ||
this.initialised_ = false | ||
if (this._finalized) throw new Error('Digest already called') | ||
this._finalized = true | ||
var digest = this._digest() | ||
if (encoding === undefined) encoding = HashBase.DEFAULT_ENCODING | ||
if (encoding !== 'buffer') digest = digest.toString(encoding) | ||
if (encoding !== undefined) digest = digest.toString(encoding) | ||
return digest | ||
@@ -50,3 +68,3 @@ } | ||
if (encoding !== 'buffer') chunk = new Buffer(chunk, encoding) | ||
this._update(chunk) | ||
this.update(chunk) | ||
} catch (err) { | ||
@@ -53,0 +71,0 @@ error = err |
{ | ||
"name": "hash-base", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "abstract base class for hash-streams", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -11,8 +11,21 @@ # hash-base | ||
Requires you to implement 2 methods: | ||
## Example | ||
- `_update(data)` takes a buffer and should return nothing | ||
```js | ||
function MyHash () { | ||
HashBase.call(64) // in bytes | ||
} | ||
- `_digest()` takes no arguments and should return a buffer | ||
inherti(MyHash, HashBase) | ||
MyHash.prototype._update = function () { | ||
// hashing one block with buffer this._block | ||
} | ||
MyHash.prototype._digest = function () { | ||
// create padding and produce result | ||
} | ||
``` | ||
You also can check [source code](index.js) or [crypto-browserify/md5.js][5] | ||
## LICENSE | ||
@@ -26,1 +39,2 @@ | ||
[4]: https://github.com/crypto-browserify/cipher-base | ||
[5]: https://github.com/crypto-browserify/md5.js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4294
66
39