Node.js – hash.copy() 方法


雜湊 是用於建立資料雜湊摘要的諸多實用類之一。hash.copy() 方法建立一個新的雜湊物件,其中包含當前雜湊物件的內部狀態的深層副本。

語法

hash.copy([options])

引數

  • 選項 −此輸入引數輸入用於控制流行為,因此將包含 stream.tranformOptions

示例 1

建立檔案"hashCopy.js"並複製以下程式碼片段。建立檔案後,使用命令"node hashCopy.js"執行此程式碼。

// hash.update() demo Example

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

// Defining the hash
const hash = crypto.createHash('sha256');

// Updating the hash value
hash.update('Welcome to TutorialsPoint');
// Displaying the hash value after copying
console.log("Hash is: " + hash.copy.digest('hex'));

輸出

C:\home
ode>> node hashCopy.js Hash is: c1e6fe9c48a1cf16fa6928053975cf3d987619ca0992ac20861c32a9fa0d5d17

示例 2

// hash.update() demo Example

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

// Defining the hash
const hash = crypto.createHash('sha256');

// Updating the hash value
hash.update('Tutorials Point - SIMPLY LEARNING');

// Creating copies
const copy1 = hash.copy();
const copy2 = hash.copy();

// Displaying the hash value after copying
console.log("Original Hash is: " + hash.digest('hex'));
console.log("Copy1 Hash is: " + copy1.digest('hex'));
console.log("Copy2 Hash is: " + copy2.digest('hex'));

輸出

C:\home
ode>> node hashCopy.js Original Hash is: 5f7a802a94340899861ac3babd895e9c7fa8240dc8f5cf87144072d456d73a05 Copy1 Hash is: 5f7a802a94340899861ac3babd895e9c7fa8240dc8f5cf87144072d456d73a05 Copy2 Hash is: 5f7a802a94340899861ac3babd895e9c7fa8240dc8f5cf87144072d456d73a05

更新時間:2021 年 8 月 16 日

216 次瀏覽

開啟你的 職業 生涯

完成培訓課程以獲取認證

開始
廣告
© . All rights reserved.