Skip to content
Related Articles

Related Articles

Improve Article

Node.js hash.update() Method

  • Last Updated : 10 May, 2021

The hash.update( ) method is an inbuilt function of the crypto module’s Hash class. This is used to update the hash with given data. This method can be called multiple times to update the content of the hash as this method can take streaming data, such as file read stream.

This function takes data as an argument to generate the hash, this can be a string or file object. Along with the data, this also takes encoding type for the data, this can be utf-8, binary, or ASCII. If encoding is not provided and data is a string then utf-8 is used.desired output length in bytes.

Module Installation: Install the required module using the following command:

npm install crypto

Syntax:

hash.update(data [,Encoding])

Parameter: This function takes the following two parameters:



  • data: Data that needs to be added to the hash.
  • encoding: Encoding type for the data.

Return Value: This method returns an object with updated data.

Example 1:

Javascript




// Import crypto module
const crypto = require('crypto');
  
// Create Hash instance with createHash
var hash = crypto.createHash('sha256')
                            // Use update to add data
                            .update('I love GeeksForGeeks')
  
                            // Use digest to get the hash value
                            .digest('hex');
  
// Prints the hash value
console.log("Hash Value : " + hash);

Output:

Hash Value : 5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196

Example 2: 

Javascript




// Import crypto module
const crypto = require('crypto');
  
// Create Hash instance with createHash
var hash = crypto.createHash('sha256')
                            // Use update to add data
                            .update('I love GeeksForGeeks')
  
                            // Use update to add data
                            .update('Because I love coding')
  
                            // Use digest to get the hash value
                            .digest('hex');
  
// Prints the hash value
console.log("Hash Value : " + hash);

Output:

Hash Value : e0789790d7da870830a679828c722f74f3840d4a6483f5babfb62c4d19884c9e

Reference: https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding




My Personal Notes arrow_drop_up
Recommended Articles
Page :