title: NodeJS hash files recursively in a directory
body: <p>I am able to achieve recursive file traversal in a directory (i.e to explore all the subdirectories and files in a directory). For that I have used an <a href="https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search/5827895#5827895">answer</a> from a respective post on stack overflow. The snippet of that is below:</p>
  
  <pre><code>var fs = require("fs");
  
  var tree = function(dir, done) {
    var results = {
          "path": dir,
          "children": []
        };
    fs.readdir(dir, function(err, list) {
      if (err) { return done(err); }
      var pending = list.length;
      if (!pending) { return done(null, results); }
      list.forEach(function(file) {
        fs.stat(dir + '/' + file, function(err, stat) {
          if (stat &amp;&amp; stat.isDirectory()) {
            tree(dir + '/' + file, function(err, res) {
              results.children.push(res);
              if (!--pending){ done(null, results); }
            });
          } else {
            results.children.push({"path": dir + "/" + file});
            if (!--pending) { done(null, results); }
          }
        });
      });
    });
  };
  
  module.exports = tree;
  </code></pre>
  
  <p>When I run:</p>
  
  <pre><code> tree(someDirectoryPath, function(err, results) {
          if (err) throw err;
  
          console.log(results);
        });
  </code></pre>
  
  <p>I get a sample result, such as this one:</p>
  
  <pre><code>{ path: '/Users/UserName/Desktop/1',
    children: 
     [ { path: '/Users/UserName/Desktop/1/file1' },
       { path: '/Users/UserName/Desktop/1/file2' },
       { path: '/Users/UserName/Desktop/1/file3' },
       { path: '/Users/UserName/Desktop/1/subdir1',
         children: [Object] } ] }
  </code></pre>
  
  <p>I am also able to hash a single file in a specific location, by using the fs' module ReadStream method. The snippet for that is below:</p>
  
  <pre><code>/**
   * Checking File Integrity
   */
  var fs = require('fs'),
        args = process.argv.splice('2'),
        path = require('path'),
        traverse = require('/Users/UserName/Desktop/tree.js'),
        crypto = require('crypto');
  //var algorithm = ['md5', 'sha1', 'sha256', 'sha512'];
  var algorithm = 'sha512';
  var hashTable = new Array();
  
          var hash = crypto.createHash(algorithm);
  
          var fileStream = fs.ReadStream(args[0]);
  
          fileStream.on('data', function(data) {
                  hash.update(data);
          fileStream.on('end', function() {
                  var digest = hash.digest('hex');
                  console.log('algorithm used: ', algorithm);
                  console.log('hash for the file: ',digest);
                  hashTable[args[0]] = digest;
                  console.log(hashTable);
          });
  });
  </code></pre>
  
  <p>Where args[0] stores the location of the file to be read by the ReadStream. After hashing of a specific file, the console log returned is as follows:</p>
  
  <pre><code>node fileIntegrityChecker.js hello.txt
  algorithm used:  sha512
  hash for the file:  9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
  the hashtable is: [ 'hello.txt': '9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043' ]
  </code></pre>
  
  <p><strong>My problem is</strong> that I tried to somehow integrate the tree module functionality in the hash related js file. My idea is that the program will capture the user's input, <em>as a path to a directory</em> and that input will be processed to traverse the whole subdirectories and files of a folder. Also, the <code>fileStream.on</code> method should be included in the callback from the tree module. However I am not fully initiated in the callback mechanism and I hope to get some insight from you. </p>
  
  <p><strong>This is what I've tried</strong></p>
  
  <pre><code>/**
   * Checking File Integrity
   */
  var fs = require('fs'),
        args = process.argv.splice('2'),
        path = require('path'),
        tree = require('/Users/UserName/Desktop/tree.js'),
        crypto = require('crypto');
  //var algorithm = ['md5', 'sha1', 'sha256', 'sha512'];
  var algorithm = 'sha512';
  var hashTable = new Array();
  
          var pathString = 'Users/UserName/Desktop/1';
          tree(pathString, function(err, results) {
              if (err) throw err;
  
              var hash = crypto.createHash(algorithm);
              var fileStream = fs.ReadStream(results.children[1]['path']);
               fileStream.on('data', function(data) {
                  hash.update(data);
               fileStream.on('end', function() {
                  var digest = hash.digest('hex');
                  console.log('algorithm used: ', algorithm);
                  console.log('hash for the file: ',digest);
                  hashTable[results.children[1]['path']] = digest;
                  console.log('The hashtable is: ', hashTable);
                  });
              });
          });
  </code></pre>
  
  <p>Now, I've made some progress in the sense that I don't receive an error. Basically I achieved my scope. <strong>However</strong> I am able to extract only <strong>one</strong> result explicitly. For some reason, I cannot think how to iteratively (for instance) get each child of the result JSON object. If that is solved, I think the problem will be completely solved.</p>
  
  <p><strong>Can you please show me a way how to successfully combine the module and the js file to recursively traverse all the contents of a directory and create a hash for every file in it. I need this to ultimately check if some changes in the files occurred, based on their hashes. Thank you!</strong></p>