SHA256 Hashing algorithm in node.js


Secure Hash Algorithm 256 comes under SHA2 and it is a cryptographic hash function which is used to generate hash values.It produces a 256-bit hash value which is known as message digest. Code snippet is given below :


//Name of the file : sha256-hash.js
//Loading the crypto module in node.js
var crypto = require('crypto');
//creating hash object 
var hash = crypto.createHash('sha256');
//passing the data to be hashed
data = hash.update('nodejsera', 'utf-8');
//Creating the hash in the required format
gen_hash= data.digest('hex');
//Printing the output on the console
console.log("hash : " + gen_hash);



  • Now run the snippet using the following command :
    >node sha256-hash.js
    hash : 664ad54634c10149e324ffd83bd7b90badbffffcc5738c602b3e27cb7617737f