Skip to content
关注公众号,获取新课通知

crypto 数据加密


  1. 安装
shell
npm install crypto --save
  1. 配置文件配置 config / config.default.js
js
config.crypto = {
  secret: "qhdgw@45ncashdaksh2!#@3nxjdas*_672",
};
  1. 使用
js
// 引入
const crypto = require('crypto');

// 加密
async createPassword(password) {
    const hmac = crypto.createHash("sha256", this.app.config.crypto.secret);
    hmac.update(password);
    return hmac.digest("hex");
}

// 验证密码
async checkPassword(password, hash_password) {
    // 先对需要验证的密码进行加密
    password = await this.createPassword(password);
    return password === hash_password;
}