Generating HMAC using sha256 hashing algorithm


Overview

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. HMAC involves hashing with the help of a secret key as shown in the snippet below :

Code


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

													
												

Run

  • Now run the snippet using the following command :
    													
    >node sha256-hmac.js
    hmac : 89365e7dc5bde2be58737b7f6086275e4284506f00c74b5822b7b7afdb93a7a9