hash-base
Advanced tools
Comparing version 3.0.3 to 3.0.4
21
index.js
'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 | ||
@@ -41,5 +48,5 @@ this._blockOffset = 0 | ||
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) | ||
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding) | ||
@@ -66,3 +73,3 @@ // consume data | ||
HashBase.prototype._update = function (data) { | ||
HashBase.prototype._update = function () { | ||
throw new Error('_update is not implemented') | ||
@@ -77,2 +84,8 @@ } | ||
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 | ||
@@ -79,0 +92,0 @@ } |
{ | ||
"name": "hash-base", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "abstract base class for hash-streams", | ||
@@ -30,9 +30,13 @@ "keywords": [ | ||
"dependencies": { | ||
"inherits": "^2.0.1" | ||
"inherits": "^2.0.1", | ||
"safe-buffer": "^5.0.1" | ||
}, | ||
"devDependencies": { | ||
"nyc": "^7.0.0", | ||
"standard": "^7.0.0", | ||
"nyc": "^8.3.2", | ||
"standard": "*", | ||
"tape": "^4.2.0" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
} | ||
} |
@@ -14,4 +14,10 @@ # hash-base | ||
```js | ||
const HashBase = require('hash-base') | ||
const inherits = require('inherits') | ||
// our hash function is XOR sum of all bytes | ||
function MyHash () { | ||
HashBase.call(64) // in bytes | ||
HashBase.call(this, 1) // in bytes | ||
this._sum = 0x00 | ||
} | ||
@@ -22,8 +28,12 @@ | ||
MyHash.prototype._update = function () { | ||
// hashing one block with buffer this._block | ||
for (let i = 0; i < this._block.length; ++i) this._sum ^= this._block[i] | ||
} | ||
MyHash.prototype._digest = function () { | ||
// create padding and produce result | ||
return this._sum | ||
} | ||
const data = Buffer.from([ 0x00, 0x42, 0x01 ]) | ||
const hash = new MyHash().update(data).digest() | ||
console.log(hash) // => 67 | ||
``` | ||
@@ -30,0 +40,0 @@ You also can check [source code](index.js) or [crypto-browserify/md5.js][5] |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6028
75
49
2
1
+ Addedsafe-buffer@^5.0.1
+ Addedsafe-buffer@5.2.1(transitive)