Node.js – hash.update() Method


The Hash class is one of the many utility classes that is used for creating the hash digests of data. The hash.update() method updates the hash content with the data passed, and the encoding that is passed along with the parameters. If the encoding is not passed and the data is a string, ‘utf8’ encoding is used.

Syntax

hash.update(data, [inputEncoding])

Parameters

The parameters are described below −

  • data − This input parameter takes input for the data that will update the hash content.

  • InputEncoding − Encoding to encode the input data or the data string

Example 1

Create a file with the name "hashUpdate.js" and copy the following code snippet. After creating the file, use the command "node hashUpdate.js" to run this code

 Live Demo

// hash.update() demo Example

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

// Creating a hash instance with the below values
var hash = crypto.createHash('sha256')

   // Data to update the hash
   .update('Welcome to TutorialsPoint !')

   // Using digest to get its hex value
   .digest('hex');

// Printing the hash value
console.log("Hash Value: " + hash);

Output

C:\home
ode>> node hashUpdate.js Hash Value: 5f55ecb1ca233d41dffb6fd9e307d37b9eb4dad472a9e7767e8727132b784461

Example 2

Let’s have a look at one more example

 Live Demo

// hash.update() demo Example

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

// Creating a hash instance with the below values
var hash = crypto.createHash('sha256')

   // Data to update the hash
   .update('Welcome to TutorialsPoint !')

   // We can update the hash multiple times
   .update('SIMPLY LEARNING')

   // Using digest to get its base64 value
   .digest('base64');

// Priniting the hash value
console.log("Base64 Value: " + hash);

Output

C:\home
ode>> node hashUpdate.js Base64 Value: WdXHoQhqYk4EBEXYBuvmRFGdid+xnxUk22YACiYtnIk=

Updated on: 18-Aug-2021

360 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements