Node.js - hmac.digest() 方法
Hmac 類是用於建立加密 HMAC 摘要的許多實用程式類之一。Hmac.digest() 方法用於計算使用 Hmac.update() 方法更新的所有資料。如果提供了編碼,將返回一個字串,否則返回一個緩衝區。
語法
hmac.digest( [encoding] )
引數
- encoding − 此輸入引數接受編碼的輸入作為考慮計算 hmac 時的考慮因素。
示例 1
建立一個檔案 "hmacDigest.js" 並複製以下程式碼片段。建立檔案後,使用命令 "node hmacDigest.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 to Tutorials point');
// Printing the hmac value using digests
console.log("Hmac is: " + hmac.digest('hex'))輸出
C:\home
ode>> node hmacDigest.js Hmac is: 4e6fa9b98ed4c498a498148bd720cc7b14b40b148e5b919bc89869b8c5dd5c9e
示例 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');
// Defining the hmac encoding algorithm
var encoding = "sha256"
// Defining the secret key
var secretKey = "1234567890"
// Defining the data to be hashed
var data = "TutorialsPoint"
// Creating the Hmac in hex encoding
let hmacDigest = crypto.createHmac(encoding, secretKey).update(data).digest("hex")
// Creating the Hmac in base64 encoding
let hmacDigestWithBase64 = crypto.createHmac(encoding, secretKey).update(data).digest("base64")
// Printing the Hmac value using digests
console.log("Hmac in Hex is: " + hmacDigest)
console.log("Hmac in Base64 encoding: " + hmacDigestWithBase64)輸出
C:\home
ode>> node hmacDigest.js Hmac in Hex is: 05fa1c5566678274ca0a4db70b0522cbb765140fa5903fbd42c1eac8682538dd Hmac in Base64 encoding: BfocVWZngnTKCk23CwUiy7dlFA+lkD+9QsHqyGglON0=
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP