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); } },