title: nodejs crypto.createhash('sha256') produces different hash for the same S3 object
body: <p>Every time I test my code it produces a different hash even though it is the same file/object from S3.  Here is my code:</p>
  <pre><code>&quot;use strict&quot;;
  var crypto = require('crypto');
  let AWS    = require(&quot;aws-sdk/global&quot;);
  AWS.config.update({region: &quot;us-east-1&quot;});
  let S3 = require(&quot;aws-sdk/clients/s3&quot;);
  let s3 = new S3();
  
  const successResponse = { statusCode: 200, body: JSON.stringify('Processed File Metadata successfully')};
  
  // Main Lambda entry point
  exports.handler = (event, context, callback) =&gt; {
    context.callbackWaitsForEmptyEventLoop = false;
  
    var bucketName = event.Records[0].s3.bucket.name;
    var objectKey  = event.Records[0].s3.object.key;
  
    getS3ObjectHash(bucketName, objectKey, function(err, s3ObjectHash){
      if(err){
        console.log(&quot;Error getting S3ObjectHash&quot;+err.message);
        return callback(err);
      }else{
        return callback(null, successResponse);
      }
    });
  };
  
  async function getS3ObjectHash(srcBucket, srcKey, callback){
    let hashResult;
    try {
      console.log(&quot;Bucket &quot;+srcBucket);
      console.log(&quot;Key &quot;+srcKey);
      const params = {
        Bucket: srcBucket,
        Key: srcKey
      };
        // Creating Hash to be used as the Key for S3 Object
      let hash = crypto.createHash('sha256');
      
      let stream = s3.getObject(params, function(err, data){
        if(err){
          console.log(err);
          return;
        }
      }).createReadStream();
      stream.on('data', function(data){
        hash.update(data, 'binary');
      });
      stream.on('end', function(){
        hashResult = hash.digest('hex');
        console.log(&quot;**** Result hash &quot;+ hashResult);
      return callback(null, hashResult);
      });
    } catch (error) {
        console.log(&quot;Caught error &quot;+ error.message);
        return callback(error);
    } 
  }
  </code></pre>
  <p>Here is the test data that I'm using. It is the S3 trigger event when creating an object :</p>
  <pre><code>{
    &quot;Records&quot;: [
      {
        &quot;eventVersion&quot;: &quot;2.1&quot;,
        &quot;eventSource&quot;: &quot;aws:s3&quot;,
        &quot;awsRegion&quot;: &quot;us-east-2&quot;,
        &quot;eventTime&quot;: &quot;2019-09-03T19:37:27.192Z&quot;,
        &quot;eventName&quot;: &quot;ObjectCreated:Put&quot;,
        &quot;userIdentity&quot;: {
          &quot;principalId&quot;: &quot;AWS:PRINCIPAL-ID&quot;
        },
        &quot;requestParameters&quot;: {
          &quot;sourceIPAddress&quot;: &quot;205.255.255.255&quot;
        },
        &quot;responseElements&quot;: {
          &quot;x-amz-request-id&quot;: &quot;D82B88E5F771F645&quot;,
          &quot;x-amz-id-2&quot;:  &quot;vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo=&quot;
        },
        &quot;s3&quot;: {
          &quot;s3SchemaVersion&quot;: &quot;1.0&quot;,
          &quot;configurationId&quot;: &quot;828aa6fc-f7b5-4305-8584-487c791949c1&quot;,
          &quot;bucket&quot;: {
            &quot;name&quot;: &quot;BUCKET-NAME&quot;,
            &quot;ownerIdentity&quot;: {
              &quot;principalId&quot;: &quot;PRINCIPAL-ID&quot;
        },
            &quot;arn&quot;: &quot;arn:aws:s3:::BUCKET-NAME&quot;
          },
          &quot;object&quot;: {
            &quot;key&quot;: &quot;utp/1185481445975.nrcs143_023308.pdf&quot;,
            &quot;size&quot;: 1305107,
            &quot;eTag&quot;: &quot;b21b84d653bb07b05b1e6b33684dc11b&quot;,
            &quot;sequencer&quot;: &quot;0C0F6F405D6ED209E1&quot;
          }
        }
      }
    ]
  }
  </code></pre>
  <p>Here is the output from two consecutive tests using the same above test data</p>
  <pre><code>2020-06-21 INFO Key utp/1185481445975.nrcs143_023308.pdf
  2020-06-21 INFO **** Result hash ea41e18defef3f4e44743d847ed804aa818afb33765d7ea83d0d2d92138e5946
  
  2020-06-21 INFO Key utp/1185481445975.nrcs143_023308.pdf
  2020-06-21 INFO **** Result hash b226d051e637f7627acbab588ab171eebefad412c797a7e7cf66e92f924e578f
  </code></pre>
  <p>As you can see the Result Hash is different for each test.</p>
  <p>I know that the test is the same.</p>
  <p>I know that the S3 object is the same.</p>
  <p>I know I'm <strong>not so good</strong> with Nodejs. Could someone please tell me what I'm doing wrong?</p>