- A+
所属分类:MongoDB
前面的话
比来在做的小我项目中,必要对暗码进行加密保留,对该操作的具体步调记载如下
先容
关于mongoose已经写过博客就不再赘述,下面主要先容bcrypt
bcrypt是一个由两个外国人依据Blowfish加密算法所设计的暗码散列函数。实现中bcrypt会使用一个加盐的流程以防御彩虹表进击,同时bcrypt照样顺应性函数,它可以借由增长迭代之次数来抵抗暴力破解法
使用npm安装即可
npm install --save bcrypt
用户模子
下面来创立代码用户user的schema,用户名不克不及反复
var mongoose = require('mongoose'), Schema = mongoose.Schema, bcrypt = require('bcrypt');var UserSchema = new Schema({ username: { type: String, required: true, index: { unique: true } }, password: { type: String, required: true } }); module.exports = mongoose.model('User', UserSchema);
加密
下面参加用户模子的是Mongoose的中央件,该中央件使用pre前置钩子,在暗码保留之前,主动地把暗码酿成hash。具体代码如下
let SALT_WORK_FACTOR = 5
UserSchema.pre('save', function(next) {
var user = this;
//发生暗码hash当暗码有变动的时刻(或者是新暗码)
if (!user.isModified('password')) return next();
// 发生一个salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
// 联合salt发生新的hash
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
// 使用hash笼罩明文暗码
user.password = hash;
next();
});
});
});
在node.bcrypt.js中SALT_WORK_FACTOR默认使用的是10,这里设置为5
验证
加密之后,暗码原文被替换为密文了。我们无法解密,只能经由过程bcrypt的compare办法,对再次传入的暗码和数据库中保留的加密后的暗码进行比拟,假如匹配,则登录胜利
UserSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
把上面的几个步调串在一路,完备代码如下
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
bcrypt = require('bcrypt'),
SALT_WORK_FACTOR = 5;
var UserSchema = new Schema({
username: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true }
});
UserSchema.pre('save', function(next) {
var user = this;
// only hash the password if it has been modified (or is new)
if (!user.isModified('password')) return next();
// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
// hash the password using our new salt
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
// override the cleartext password with the hashed one
user.password = hash;
next();
});
});
});
UserSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
module.exports = mongoose.model('User', UserSchema);
测试
把上面的代码保留成user-model.js,然后运行下面代码来现实测试
var mongoose = require('mongoose'),
User = require('./user-model');
var connStr = 'mongodb://localhost:27017/mongoose-bcrypt-test';
mongoose.connect(connStr, function(err) {
if (err) throw err;
console.log('Successfully connected to MongoDB');
});
// create a user a new user
var testUser = new User({
username: 'jmar777',
password: 'Password123'
});
// save user to database
testUser.save(function(err) {
if (err) throw err;
// fetch user and test password verification
User.findOne({ username: 'jmar777' }, function(err, user) {
if (err) throw err;
// test a matching password
user.comparePassword('Password123', function(err, isMatch) {
if (err) throw err;
console.log('Password123:', isMatch); // -> Password123: true
});
// test a failing password
user.comparePassword('123Password', function(err, isMatch) {
if (err) throw err;
console.log('123Password:', isMatch); // -> 123Password: false
});
});
});
节制台中输入如下数据:
数据库数据如下:
以上这篇使用mongoose和bcrypt实现用户暗码加密的示例便是小编分享给年夜家的全体内容了,愿望能给年夜家一个参考,也愿望年夜家多多支撑剧本之家。
您可能感兴致的文章:
MongoDB优化心得分享

