Using SHA-256 with NodeJS Crypto

Cameron picture Cameron · Jan 15, 2015 · Viewed 85.5k times · Source

I'm trying to hash a variable in NodeJS like so:

var crypto = require('crypto');

var hash = crypto.createHash('sha256');

var code = 'bacon';

code = hash.update(code);
code = hash.digest(code);

console.log(code);

But looks like I have misunderstood the docs as the console.log doesn't log a hashed version of bacon but just some information about SlowBuffer.

What's the correct way to do this?

Answer

MaximeF picture MaximeF · Jan 15, 2015

base64:

const hash = crypto.createHash('sha256').update(pwd).digest('base64');

hex:

crypto.createHash('sha256').update(pwd).digest('hex');