crypto.createHash() Method in Node.js

Node.jsJavascriptWeb DevelopmentFront End Technology

The crypto.createHash() method will create a hash object and then return it. THis hash object can be used for generating hash digests by using the given algorithm. The optional options are used for controlling the stream behaviour. For some hash functions like XOF and 'shake256' the output length is used for specifying the desired output length in bytes.


crypto.createHash(algorithm, [options])


The above parameters are described as below −

  • algorithm – This algorithm is used for generating the hash digests. Input type is string.

  • options – These are optional parameters which can be used for controlling the stream behaviour.


Create a file with name – createHash.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node createHash.js


 Live Demo

// crypto.createHash() demo example

// Importing crypto module
const crypto = require('crypto');

// Deffining the secret key
const secret = 'TutorialsPoint';

// Initializing the createHash method using secret
const hashValue = crypto.createHash('sha256', secret)

   // Data to be encoded
   .update('Welcome to TutorialsPoint !')

   // Defining encoding type
// Printing the output
console.log("Hash Obtained is: ", hashValue);


C:\home\node>> node createHash.js
Hash Obtained is:


Let's take a look at one more example.

 Live Demo

// crypto.createHash() demo example

// Importing crypto module
const crypto = require('crypto');
const fs = require('fs');

// Getting the current file path
const filename = process.argv[1];

// Creting hash for current path using secret
const hash = crypto.createHash('sha256', "TutorialsPoint");

const input = fs.createReadStream(filename);
input.on('readable', () => {
   // Reading single element produced by hash stream.
   const val =;
   if (val)
   else {
      console.log(`${hash.digest('hex')} ${filename}`);


C:\home\node>> node createHash.js
Published on 20-May-2021 11:53:02