6 - Hashing files

Now let's expand on our previous, simple hashing exercise and hash the content of a file instead a simple string (this is a file-sharing workshop after all ;).

Solution of exercise 5

var crypto = require('crypto')

var string = 'Web Rebels 2017'
var hash = crypto.createHash('sha256')

// Add the string to the hash object to encode it
hash.update(string)

// Get the hashed version of the string as a Buffer
var digest = hash.digest()

console.log('%s => %s', string, digest.toString('hex'))

Assignment

Write a program that outputs the hash of a file

Tips

Use what you've learned about reading files as streams to update the hash object.

Use the data event to update the hash, i.e:

fileStream.on('data', function (chunk) {
  // TODO: update the hash with the given chunk
})

Testing

Run the program multiple times and check that the same hash is outputted for the same file and different hashes for different files.

When you are done click here to go to the next exercise