Bcrypt不散列

时间:2018-10-19 12:09:15

标签: javascript mongodb express

我正在尝试学习一些Express.js,现在我有一些从React前端发送到我的Express服务器的表单,并且正在将这些数据插入MongoDB模式中。在一些在线教程之后,我尝试使用bcrypt对插入的PIN码(这是一个尚未达到生产btw的系统)进行哈希处理,但是始终使用控制台日志明文保存数据,不进行任何加密,我还看到了该PIN代码不进行哈希处理。

我的哈希代码驻留在我的mongoDB模型中,这是模型

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

const Schema = mongoose.Schema;

// Creates the needed schema
let userSchema = new Schema({
  name: String,
  created_at: Date,
  updated_at: Date,
  balance: Number,
  address: String,
  ssn: Number,
  bankNumber: Number,
  cards: [
    {
      formType: String, // Visa eller Mastercard
      cardNumber: Number,
      cvc: Number,
      expirationDate: Date,
      pin: Number,
      status: Boolean,
    }
  ],
  whitdrawal: [
    {
      amount: Number,
      date: Date, 
      reason: String
    }
  ]
});
// Inserts
userSchema.pre('save', function(next) {
  const currentDate = new Date();
  // 10 defines salt rounds
  let pin = this.cards[0].pin
  bcrypt.hash(pin, 10, function(err,hash){
    if(err){
      return next(err); 
    }
    pin = hash; 
  })
  this.updated_at = currentDate;
  this.date = currentDate;
  this.pin = pin; 
  console.log("Pin is " + pin)
  if (!this.created_at) this.created_at = currentDate;
  next();
});

// Creates model for schema
const AtmUser = mongoose.model('AtmUser', userSchema);

// Export so it is available for the rest of the application
module.exports = AtmUser;

它很好地将数据保存到架构中,只是不对引脚进行加密。在userSchema.pre中从服务器设置日期有效。

我很乐意发布任何其他代码。

1 个答案:

答案 0 :(得分:1)

问题是bcrypt.hash(..., function(err, hash) { ... }) 异步

回调提供的回调

因此

this.updated_at = currentDate;
this.date = currentDate;
this.pin = pin; 
console.log("Pin is " + pin)
// etc

将在

之前执行
pin = hash; 

有机会跑步。

共有三个选项

正确使用回调,将所有依赖于hash的代码放在回调中

userSchema.pre('save', function(next) {
    const currentDate = new Date();
    // 10 defines salt rounds
    let pin = this.cards[0].pin;
    bcrypt.hash(pin, 10, (err, pin) => {
        if (!err) {
            this.updated_at = currentDate;
            this.date = currentDate;
            this.pin = pin; 
            console.log("Pin is " + pin)
            if (!this.created_at) this.created_at = currentDate;
        }
        next(err);
    })
});

上面没有箭头功能(但是您在代码中使用let,所以我希望您知道箭头功能,以防万一)

userSchema.pre('save', function(next) {
    const currentDate = new Date();
    // 10 defines salt rounds
    let pin = this.cards[0].pin;
    let _this = this; // save _this for the callback
    bcrypt.hash(pin, 10, function(err, pin) {
        if (!err) {
            _this.updated_at = currentDate;
            _this.date = currentDate;
            _this.pin = pin; 
            console.log("Pin is " + pin)
            if (!_this.created_at) _this.created_at = currentDate;
        }
        next(err);
    })
});

或使用承诺

userSchema.pre('save', function(next) {
    const currentDate = new Date();
    // 10 defines salt rounds
    let pin = this.cards[0].pin
    bcrypt.hash(pin, 10).then((pin) => {
        this.updated_at = currentDate;
        this.date = currentDate;
        this.pin = pin; 
        console.log("Pin is " + pin)
        if (!this.created_at) this.created_at = currentDate;
        next();
    }).catch((err) => {
        next(err); 
    })
});

最后,使用async / await

userSchema.pre('save', async function(next) {
    const currentDate = new Date();
    // 10 defines salt rounds
    let pin = this.cards[0].pin
    try {
        pin = await bcrypt.hash(pin, 10);
    } catch(err) {
        return next(err); 
    }
    this.updated_at = currentDate;
    this.date = currentDate;
    this.pin = pin; 
    console.log("Pin is " + pin)
    if (!this.created_at) this.created_at = currentDate;
    next();
});

还有第四种选择,但没有充分的理由同步.hash