Skip to content

A PassThrough stream that taps to chunks flow and calculates the hash digest on the fly.

License

Notifications You must be signed in to change notification settings

heroqu/hash-through

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hash-through

A PassThrough stream that taps to chunks flow and calculates the digest on the fly.

The hashing function has to be supplied. It should be in a form similar to createHash from node crypto module.

Setup

npm install hash-through

Usage

A short example using createHash from node's crypto module:

const HashThrough = require('hash-through')
const crypto = require('crypto')

function createHash(){
  // here we are wrapping a particular implementation
  return crypto.createHash('sha256')
}

const ht = HashThrough(createHash)

// Now we can pipe through and get the digest ready
// by the very time the streaming is over:

const fs = require('fs')
const src = fs.createReadStream(__filename)

src.pipe(ht).pipe(process.stdout)

ht.on('finish', ()=>{
  console.log(ht.digest('hex'))
})

Basically, createHash should be a function with no args that returns a hash object, which in turn should have two methods: hash.update(chunk) and hash.digest(format) with the same meaning as in crypto module

More examples

The test directory contains some more examples of how to plug in different hash algorithms, including both cryptographic functions from node crypto module:

  • sha256
  • sha512
  • ripemd160

as well as some non-cryptographic ones:

Dependencies

For "a stable streams base, regardless of what version of Node you are using" we use readable-stream standalone stream module instead of Node core implementation (read elaboration on this here).

The idea

The point of this module is that original stream can really pass through the hashing instance 'untouched', so it really is a PassThrough stream from external point of view. One can insert one or more instances of it at any points of piping chains without violating the existing streaming logics. Kind of like tapping to the wire or playing man-in-the-middle.