Open In App

Node.js hash.copy() Method

Last Updated : 10 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The hash.copy( ) method is an inbuilt function of the crypto module’s Hash class. This method is used to make a copy of the current state of the hash. This method can be called multiple times to create multiple copies of the hash. This method will throw an error if called after the digest method has been called.

This function takes an optional argument to control stream behavior like Output length.

Syntax:

hash.copy([,Optional ])

Parameter: This function takes one parameter which is optional:

  • .Stream behavior of data.

Return Value: This method returns the copy of the current state of the hash.

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

npm install crypto

Example 1: Make a copy of the hash just a single time.

index.js




// Importing crypto module
const crypto = require('crypto');
  
// Creating Hash instance with createHash
const hash = crypto.createHash('sha256');
  
// use update to add data
hash.update('I love GeeksForGeeks');
  
// Making the copy of the current hash
const hashCopy = hash.copy();
  
// Printing the hash value
console.log("Original Hash Value : " + hash.digest('hex'));
console.log("Copied Hash Value : " + hashCopy.digest('hex'));


Run index.js file using below command:

node index.js

Output:

Original Hash Value : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196
Copied Hash Value : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196

Example 2: Make a copy of the hash multiple times.

index.js




const crypto = require('crypto');
  
// Creating Hash instance with createHash
const hash = crypto.createHash('sha256');
  
// Adding data to hash
hash.update('I love GeeksForGeeks');
  
// Making copy of the current hash
const hashCopy1 = hash.copy();
const hashCopy2 = hash.copy();
  
// Printing the hash value
console.log("Original Hash Value : " + hash.digest('hex'));
console.log("Copy 1 : " + hashCopy1.digest('hex'));
console.log("Copy 2 : " + hashCopy2.digest('hex'));


Run index.js file using below command:

node index.js

Output:

Original Hash Value : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196
Copy 1 : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196
Copy 2 : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196

Example 3: Update Copied hash value.

index.js




//Importing crypto module
const crypto = require('crypto');
  
// Creating Hash instance with createHash
const hash = crypto.createHash('sha256');
  
// Adding data to hash
hash.update('I love GeeksForGeeks');
  
// Making copy of the current hash
const unchangedCopy = hash.copy();
const updatedCopy = hash.copy();
  
// Update the old data
updatedCopy.update('Because I love coding')
  
// Printing the hash value
console.log("Original Hash Value : " + hash.digest('hex'));
console.log("Unchanged Copy : " + unchangedCopy.digest('hex'));
console.log("Updated Copy : " + updatedCopy.digest('hex'));


Run index.js file using below command:

node index.js

Output:

Original Hash Value : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196
Unchanged Copy : 
  5a302d3c930d9e938c5326d7bb863afdc024b9ce77e30e99c4b82983350f8196
Updated Copy : 
  e0789790d7da870830a679828c722f74f3840d4a6483f5babfb62c4d19884c9e

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads