Node.js – hash.digest() 方法
Hash 类是用于创建数据哈希摘要的众多实用类之一。hash.digest() 方法计算出哈希函数中需要传递的所有数据,并将其返回。如果已定义编码,则将返回字符串;否则,将返回缓冲区。
语法
hash.digest([encoding])
参数
它采用单个参数 -
encoding - 此输入参数用于计算哈希时的要应用的编码。
示例 1
创建一个名为"hashDigest.js" 的文件,复制以下代码片段。创建文件后,使用命令 "node hashDigest.js" 运行此代码。
// hash.digest() Demo Example
// Importing the crypto module
const crypto = require("crypto")
// Creating the hash object in hex encoding
let hexDigest = crypto.createHash('sha256').update('Welcome To
TutorialsPoint').digest('hex')
// Printing the hash value using digests
console.log("Hash is: " + hexDigest)输出
C:\home
ode>> node hashDigest.js Hash is: 6c37595a919c467f0b3a1876ad0a3933cf3f7a9c3e7fc6bacf59337e0aa35afe
示例 2
让我们再看一个示例
// hash.digest() Demo Example
// Importing the crypto module
const crypto = require("crypto")
// Defining the hash encoding algorithm
let algorithm = "sha256"
// Defining the data to be hashed
let key = "TutorialsPoint"
// Creating the hash in hex encoding
let hexDigest = crypto.createHash(algorithm).update(key).digest("hex")
// Creating the hash in base64 encoding
let base64Digest =
crypto.createHash(algorithm).update(key).digest("base64")
// Printing the hash value using digests
console.log("Hex Encoding: " + hexDigest)
console.log("Base64 encoding: " + base64Digest)输出
C:\home
ode>> node hashDigest.js Hex Encoding: 62e2de2644fa0987f79f54118c175d6a924e50aa60df1ff38e197eac0da8a963 Base64 encoding: YuLeJkT6CYf3n1QRjBddapJOUKpg3x/zjhl+rA2oqWM=
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP