Using SHA-256 with NodeJS Crypto

114,926

Solution 1

base64:

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

hex:

const hash = crypto.createHash('sha256').update(input).digest('hex');

Solution 2

you can use, like this, in here create a reset token (resetToken), this token is used to create a hex version.in database, you can store hex version.

// Generate token
 const resetToken = crypto.randomBytes(20).toString('hex');
// Hash token and set to resetPasswordToken field
this.resetPasswordToken = crypto
    .createHash('sha256')
    .update(resetToken)
    .digest('hex');

console.log(resetToken )

Solution 3

nodejs (8) ref

const crypto = require('crypto');
const hash = crypto.createHash('sha256');

hash.on('readable', () => {
    const data = hash.read();
    if (data) {
        console.log(data.toString('hex'));
        // Prints:
        //  6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
    }
});

hash.write('some data to hash');
hash.end();

Solution 4

Similar to the answers above, but this shows how to do multiple writes; for example if you read line-by-line from a file and then add each line to the hash computation as a separate operation.

In my example, I also trim newlines / skip empty lines (optional):

const {createHash} = require('crypto');

// lines: array of strings
function computeSHA256(lines) {
  const hash = createHash('sha256');
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i].trim(); // remove leading/trailing whitespace
    if (line === '') continue; // skip empty lines
    hash.write(line); // write a single line to the buffer
  }

  return hash.digest('base64'); // returns hash as string
}

I use this code ensure generated lines of a file aren't edited by someone manually. To do this, I write the lines out, append a line like sha256:<hash> with the sha265-sum, and then, upon next run, verify the hash of those lines matches said sha265-sum.

Share:
114,926
Cameron
Author by

Cameron

Updated on September 04, 2021

Comments

  • Cameron
    Cameron almost 3 years

    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?

  • Dmitry Minkovsky
    Dmitry Minkovsky about 4 years
    Don't hash passwords with sha256? Look up bcrypt or something similar.
  • Ryan
    Ryan about 2 years
  • Ish
    Ish about 2 years
    Can we follow this method in angular? I tried this in an angular project and it didn't work..
  • mirichan
    mirichan about 2 years
    @Ish crypto is a Node module. Node is a server runtime. Assuming that you're running Angular in the browser, you'll need to use browser APIs: developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
  • Ish
    Ish about 2 years
    var CryptoJS = require("crypto-js"); var hmac = CryptoJS.HmacSHA256(stringToBeHashed, secretKey); var hash = hmac.toString(CryptoJS.enc.Hex); This worked for me. Thanks @mirichan