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
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐