{"id":14975630,"url":"https://github.com/xrian/mongoose-encrypt-field","last_synced_at":"2026-01-18T00:03:20.355Z","repository":{"id":48032002,"uuid":"143424375","full_name":"xrian/mongoose-encrypt-field","owner":"xrian","description":"对将要保存到 mongodb 中的字段 加解密.基于 mongoose.","archived":false,"fork":false,"pushed_at":"2021-08-10T19:09:24.000Z","size":247,"stargazers_count":1,"open_issues_count":5,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-11T10:07:15.890Z","etag":null,"topics":["crypto","encrypt","encryption","mongodb","mongoose","mongoose-plugin","nodejs","security","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xrian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-03T12:25:05.000Z","updated_at":"2019-03-09T10:36:09.000Z","dependencies_parsed_at":"2022-08-12T17:10:27.387Z","dependency_job_id":null,"html_url":"https://github.com/xrian/mongoose-encrypt-field","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xrian%2Fmongoose-encrypt-field","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xrian%2Fmongoose-encrypt-field/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xrian%2Fmongoose-encrypt-field/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xrian%2Fmongoose-encrypt-field/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xrian","download_url":"https://codeload.github.com/xrian/mongoose-encrypt-field/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217220,"owners_count":20903009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crypto","encrypt","encryption","mongodb","mongoose","mongoose-plugin","nodejs","security","typescript"],"created_at":"2024-09-24T13:52:18.683Z","updated_at":"2026-01-18T00:03:20.322Z","avatar_url":"https://github.com/xrian.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"截至目前2018-10-31 因为公司在未来的版本中,并不打算继续使用mongodb,所以,该库应该也不会在维护了.不过,如果想要实现加解密效果,可以参考该库的实现方式.至少,思路是对的\n\n## 简介\nMongoose的插件,在保存数据库前(before save)对所需字段进行加密,在查询出数据后(query after)对数据进行解密.\n\n- 对聚合查询(Aggregate)结果不会进行解密\n- 对查询条件不会进行加密(deleteMany,deleteOne,find,findOne,findOneAndDelete,findOneAndRemove,findOneAndUpdate,remove第一个参数不会加密)\n- 对populate()方法的参数也不会进行加密\n- 需要加密的字段最好为字符串类型,如果为其他类型,会调用JSON.stringify()方法再加密.\n- 加密后字段不能进行模糊查询(只能精确查询)\n- 如果需要对查询条件加密,请调用model.encryption()将字段加密后再查询\n- 如果需要对聚合查询结果解密,请调用model.decryption()方法\n\n## 已实现\n- model.find() 对查询结果解密\n- model.create() 对插入记录加密\n- model.update() 对修改内容加密\n- model.findOne() 对查询结果解密\n- model.findOneAndUpdate() 对修改内容加密，对查询结果解密\n\n## 安装\n```\nnpm i mongoose-encrypt-field --save\n```\n\n## 使用\n```\n// 导入\nconst encryptField = require(mongoose-encrypt-field).default;\nor\nimport encryptField from 'mongoose-encrypt-field';\n// use\nUserSchema.plugin(encryptField, { fields: ['username', 'email'], secret: 'this is secret'},)\n\n\nModel.find()\n// or\nModel.update()\n// 手动加密某个字符串\nconst result = Model.encryption(str);\n// 手动解密某个字符串\nconst result = Model.decryption(str);\n```\n\n## future\n- 更新操作时的加密 \n- 全面测试\n- 实现内嵌schema加密解密,以及\n- 数据迁移demo\n- 加密算法的安全性,时间复杂度和空间复杂度比较\n\n## 说明\n想要对mongodb中的字段进行加密,保存到数据库中是不可识别状态,查询出来后是正常状态\n找了几个已有的库,发现都不能满足需求,于是就花两天时间写了一个 \\\n具体实现参考了以下几个库 \\\n[mongoose-encryption](https://github.com/joegoldbeck/mongoose-encryption) \\\n[mongoose-field-encryption](https://github.com/victorparmar/mongoose-field-encryption) \\\n截止目前为止(2018-08-07),这几个库并不能实现我想要的效果,于是就自己实现了一个(造轮子:D)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxrian%2Fmongoose-encrypt-field","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxrian%2Fmongoose-encrypt-field","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxrian%2Fmongoose-encrypt-field/lists"}