Node.js – hmac.update()方法


Hmac類是用於建立雜湊訊息認證碼(HMAC)摘要的眾多實用程式類之一。Hmac.update()方法用於更新具有透過資料傳遞的Hmac內容。如果未提供編碼且資料採用字串格式,則會強制使用預設編碼“utf8”。

語法

hmac.update(data, [encoding])

引數

以下是對引數的描述

  • data − 此輸入引數將輸入作為Hmac將更新的資料。
  • encoding − 此輸入引數將輸入作為在更新Hmac時要考慮的編碼。

示例 1

建立一個名為“hmacUpdate.js”的檔案並複製以下程式碼片段。在建立檔案後,使用“node hmacUpdate.js”命令來執行此程式碼。

 線上演示

// Hmac.digest() Demo Example

// Importing the crypto module
const crypto = require("crypto")

// Initializing the Hmac object with encoding and key
const hmac = crypto.createHmac('sha256', 'secretKey');

// Updating hmac with below data
hmac.update('Welcome');
hmac.update('To');
hmac.update('TutorialsPoint')

// Printing the hmac value using digests
console.log("Hmac is: " + hmac.digest('hex'))

輸出

C:\home
ode>> node hmacUpdate.js Hmac is: 641538b74bf1a59cd9a5cbd97dd0cf3b76b572e17432997230ae346feb41d149

示例 2

 線上演示

// Hmac.digest() Demo Example

// Importing the crypto module
const crypto = require("crypto")

// Initializing the Hmac object with encoding and key
const hmac = crypto.createHmac('sha256', 'secretKey');
const hmac1 = crypto.createHmac('sha256', 'secretKey');
const hmac2 = crypto.createHmac('sha256', 'secretKey');
// Updating hmac with below data
hmac.update('TutorialsPoint');
hmac1.update('TutorialsPoint');
hmac2.update('TutorialsPoint');

// Printing the hmac value using different digests
console.log("Hmac(with hex Encoidng) is: " + hmac.digest('hex'))

console.log("Hmac(with Base64 Encoding) is: " + hmac1.digest('base64'))

console.log("Hmac(with Default Encoding) is: " + hmac2.digest())

輸出

C:\home
ode>> node hmacUpdate.js Hmac(with hex Encoidng) is: 9534f1298c89fcd4e42e3fecbb26ac66148a1a607e6fb122ef0af4859f9eb0e5 Hmac(with Base64 Encoding) is: lTTxKYyJ/NTkLj/suyasZhSKGmB+b7Ei7wr0hZ+esOU= Hmac(with Default Encoding) is: �4�)�����.?�&�f��`~o�"�􅟞��

更新於: 2021-08-17

539次瀏覽

開啟您的職業生涯

獲證完成課程

開始學習
廣告
© . All rights reserved.