对将要保存到 mongodb 中的字段 加解密.基于 mongoose.
MIT License
截至目前2018-10-31 因为公司在未来的版本中,并不打算继续使用mongodb,所以,该库应该也不会在维护了.不过,如果想要实现加解密效果,可以参考该库的实现方式.至少,思路是对的
Mongoose的插件,在保存数据库前(before save)对所需字段进行加密,在查询出数据后(query after)对数据进行解密.
npm i mongoose-encrypt-field --save
// 导入
const encryptField = require(mongoose-encrypt-field).default;
or
import encryptField from 'mongoose-encrypt-field';
// use
UserSchema.plugin(encryptField, { fields: ['username', 'email'], secret: 'this is secret'},)
Model.find()
// or
Model.update()
// 手动加密某个字符串
const result = Model.encryption(str);
// 手动解密某个字符串
const result = Model.decryption(str);
想要对mongodb中的字段进行加密,保存到数据库中是不可识别状态,查询出来后是正常状态 找了几个已有的库,发现都不能满足需求,于是就花两天时间写了一个 具体实现参考了以下几个库 mongoose-encryption mongoose-field-encryption 截止目前为止(2018-08-07),这几个库并不能实现我想要的效果,于是就自己实现了一个(造轮子:D)