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