Socket
Socket
Sign inDemoInstall

hash-base

Package Overview
Dependencies
2
Maintainers
3
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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 @@ }

12

package.json
{
"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]

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc