📜  Node.js hash.copy() 方法

📅  最后修改于: 2022-05-13 01:56:40.342000             🧑  作者: Mango

Node.js hash.copy() 方法

hash.copy()方法是加密模块的 Hash 类的内置函数。此方法用于制作散列当前状态的副本。可以多次调用此方法以创建哈希的多个副本。如果在调用摘要方法之后调用此方法,则会引发错误。

此函数采用可选参数来控制流行为,例如输出长度。

句法:

hash.copy([,Optional ])

参数:此函数采用一个可选参数:

  • .Stream 数据的行为。

返回值:此方法返回哈希当前状态的副本。

模块安装:使用以下命令安装所需模块:

npm install crypto

示例 1:只复制一次哈希。

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'));


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'));


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'));


使用以下命令运行index.js文件:

node index.js

输出:

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

示例 2:多次复制哈希。

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'));

使用以下命令运行index.js文件:

node index.js

输出:

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

示例 3:更新复制的哈希值。

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'));

使用以下命令运行index.js文件:

node index.js

输出:

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

参考:https://nodejs.org/api/crypto.html#crypto_hash_copy_options