const crypto = require('crypto')
// 密钥
const SECRET_KEY = 'WJys_5728c#'
// md5算法
function md5(content) {
// crypto.createHash(algorithm[, options]) 创建并返回 Hash 对象,该对象可用于使用给定的 algorithm 生成哈希摘要。 可选的 options 参数控制流的行为。 对于 XOF 哈希函数(例如 'shake256'),可以使用 outputLength 选项指定所需的输出长度(以字节为单位)。
const md5 = crypto.createHash('md5')
// hash.update(data[, inputEncoding]) 使用给定的 data 更新哈希内容,其编码在 inputEncoding 中给出。 如果未提供 encoding,且 data 是字符串,则强制为 'utf8' 编码。 如果 data 是 Buffer、TypedArray 或 DataView,则忽略 inputEncoding。这可以在流式传输时使用新数据多次调用
// hash.digest([encoding]) 计算传给被哈希的所有数据的摘要(使用 hash.update() 方法)。 如果提供了 encoding,则将返回字符串;否则返回 Buffer。Hash 对象在调用 hash.digest() 方法后不能再次使用。 多次调用将导致抛出错误。
return md5.update(content).digest('hex')
}
// 利用密钥生成加密密码
function genPassword(password) {
// 任意拼接字符串
const str = `password=${password}&key=${SECRET_KEY}`
// console.log('str is:', md5(str))
return md5(str)
}
// genPassword(123456)
module.exports = {
genPassword
}
<nodejs>crypto模块MD5加密
最新推荐文章于 2023-09-26 17:01:11 发布