marc walter

Generating a checksum over files or folders in node.js

2015-11-05

I needed a node.js module that generates hashes over files and folders. It should only create a hash over the text content and not over the file properties.

Further Requirements:

  • Return the same hash if the content is the same (do not check for file attributes)
  • Generate a hash over a folder structure
    • Generate the hash over a folder structure using only one loop
  • Support both promises and error-first callbacks
  • unit tests that document the behavior and not only that it does not crash

Entry folder-hash

Description

Create a hash checksum over a folder or a file.
The hashes are propagated upwards, the hash that is returned for a folder is generated over all the hashes of its children.
The hashes are generated with the sha1 algorithm and returned in base64 encoding.

The returned information looks like this:

{ name: 'test', 
  hash: 'qmUXLCsTQGOEF6p0w9V78MC7sJI=',
  children: [
    { name: 'helper', 
      hash: 'x1CX3yVH3UuLTw7zcSitSs/PbGE=',
      children: [
        { name: 'helper.js', hash: 'pHYwd8k/oZV01oABTz9MC8KovkU=' }
      ] },
    { name: 'test.js', hash: 'L/vqpdQhxmD5w62k24m4TuZJ1PM=' }
  ] 
}

Each file returns a name and a hash, and each folder returns additionally an array of children (file or folder elements).

Usage

With promises

var hasher = require('folder-hash');
// e.g. pass element path directly
hasher.hashElement(__dirname).then(function (hash) {
  console.log('Result for folder "' + __dirname + '":');
  console.log(hash.toString());
});

With callbacks

var hasher = require('folder-hash');
// e.g. pass element name and folder path separately
hasher.hashElement('node_modules', __dirname, function (error, hash)) {
  if (error) return console.error('hashing failed:', error);
  console.log('Result for folder "node_modules" in directory "' + __dirname + '":');
  console.log(hash.toString());
});

Behavior

The module meets all of my requirements. The further behavior is documented and verified in the unit tests. Execute npm test or mocha test, and have a look at the test subfolder.

Code

Available on github