egg.js 中 crypto 注册数据加密

1、安装插件

命令:npm install crypto --save

2、配置文件配置 config / config.default.js

config.crypto = {
    secret:  'qhdgw@45ncashdaksh2!#@3nxjdas*_672'
};

3、使用

// 引入
const crypto = require('crypto');

在model层中
// app/model/user.js
字段password
password: {
      type: STRING,
      allowNull: false,
      defaultValue: '',
      comment: "密码",
      set(val) {
        const hmac = crypto.createHash("sha256", app.config.crypto.secret);//const hmac = crypto.createHash("sha256",'qhdgw@45ncashdaksh2!#@3nxjdas*_672');
hmac.update(val); let hash = hmac.digest("hex"); this.setDataValue('password', hash); } },
posted @ 2020-10-09 15:32  落日残霞  阅读(594)  评论(0编辑  收藏  举报