'use strict';

let path = require('path'),
  crypto = require('crypto'),
  fs = require('fs-extra');

// Create default callback for exec
function createExecCallback(cb, quiet) {
  return function(err, stdout, stderr) {
    if (!quiet) {
      console.log(stdout);
    }
    console.log(stderr);
    cb(err);
  };
}

// for a given filename, return the sha256sum
function getSHA256(filename, cb) {
  var hashstring = 'NONE';
  var hash = crypto.createHash('sha256');
  var readStream = fs.createReadStream(filename);
  readStream.on('readable', function () {
    var chunk;
    while (null !== (chunk = readStream.read())) {
      hash.update(chunk);
    }
  }).on('end', function () {
    hashstring = hash.digest('hex');
    cb(hashstring);
  });
}

// writes to {filename}.sha256, eg., 6441cde1821c93342e54474559dc6ff96d40baf39825a8cf57b9aad264093335 requirements.json
function createSHA256File(filename, cb) {
  getSHA256(filename, function(hashstring) {
    fs.writeFile(filename + '.sha256', hashstring + ' *' + path.parse(filename).base, (err)=>{
      cb(err);
    });
  });
}

// read the existing .sha256 file and compare it to the existing file's SHA
function isExistingSHA256Current(currentFile, sha256sum, processResult) {
  if (fs.existsSync(currentFile)) {
    console.log('[INFO] \'' + currentFile + '\' exists in cache');
    getSHA256(currentFile, function(hashstring) {
      if (sha256sum !== hashstring) {
        console.log('[WARN] SHA256 in requirements.json (' + sha256sum + ') does not match computed SHA (' + hashstring + ') for ' + currentFile);
      }
      processResult(sha256sum === hashstring);
    });
  } else {
    processResult(false);
  }
}

module.exports = {
  createExecCallback,
  getSHA256,
  createSHA256File,
  isExistingSHA256Current
};

@@ -38,22 +38,18 @@
 th'),%0A  
-crypto
+fs
  = requi
@@ -56,44 +56,69 @@
 re('
-crypto'),%0A  fs = require('fs-extra')
+fs-extra');%0A%0Aimport Hash from '../browser/model/helpers/hash'
 ;%0A%0A/
@@ -418,354 +418,38 @@
 %7B%0A  
-var hashstring = 'NONE';%0A  var hash = crypto.createHash('sha256');%0A  var readStream = fs.createReadStream(filename);%0A  readStream.on('readable', function () %7B%0A    var chunk;%0A    while (null !== (chunk = readStream.read())) %7B%0A      hash.update(chunk);%0A    %7D%0A  %7D).on('end', function () %7B%0A    hashstring = hash.digest('hex');%0A    cb(hashstring);%0A  %7D
+new Hash().SHA256(filename, cb
 );%0A%7D

Use the same hash module in gulp and at runtime