SHA 512 Hashs with nodejs

Quite often you need to encrypt files. Recently I updated an application from encryption to authenticated encryption and used the encrypt-then-mac approach.

Update: I created a node module that simplifies the process for you: password-hasher

To create a hash from strings you just need a few lines in nodejs:

// generate a hash from string
var crypto = require('crypto'),
    text = 'hello bob',
    key = 'mysecret key'

// create hahs
var hash = crypto.createHmac('sha512', key)
var value = hash.digest('hex')

// print result

The great thing about the nodejs implementation of Hash is the possibility to stream data directly into the hash:

// generate a hash from file stream
var crypto = require('crypto'),
    fs = require('fs'),
    key = 'mysecret key'

// open file stream
var fstream = fs.createReadStream('./test/hmac.js');
var hash = crypto.createHash('sha512', key);

// once the stream is done, we read the values
fstream.on('end', function() {
    // print result

// pipe file to hash generator

Happy Hashing.

If you have any questions contact me via Twitter @chri_hartmann or Github

See also: