最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Bcrypt不再散列和腌制密码了

运维笔记admin10浏览0评论

Bcrypt不再散列和腌制密码了

Bcrypt不再散列和腌制密码了

在我当前应用程序的先前版本中,我有一个工作后端应用程序,使用bcrypt对我的密码进行盐分和哈希处理。在这个版本中我现在处理的是一对一的副本,具有相同的路由,控制器。所有工作都很好,来自post请求的数据保存得很好但没有哈希密码。现在显示空白密码。

我在Windows 10,64位工作,我的版本中的两个版本都是bcrypt 3.0.4本地安装。我使用mongoDB和mongoose。

我使用hashing and salting最常用的代码版本。如上所述,这仍然适用于我的老版本。

有人知道发生了什么变化吗?

这里的代码:

//relevant parts of app.js
const express = require('express');
const path = require('path');
//const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors');

// connection to mongoose 
require('./app_api/models/db');

//route to routes
const users = require('./app_api/routes/users');

//routes (post request)
router
	.route('/user/signup')
	.post(AuthenticationControllerPolicy.signupPost, ctrlUsers.signupPost); 

//fragment of the post controller
const signupPost = function (req, res) {
	//Make sure this account already exists
	Base.
		findOne({
			userName: req.body.userName
		}, function (user, err) {
			//Make sure user doesn 't already exist
			if (err) {
				return res.status(400).send({ msg: 'The email address you have entered is already associated with another account.' });
			} else { //etc..


//Create and save the user
user = new Base({
password: req.body.password
});
user.save(function (err) {

// base model with hashing and salting code
const baseSchema = new mongoose.Schema({
	password: { type: String, required: true }
	}, options);

const Base = mongoose.model('Base', baseSchema);

// salting and hashing
						

// hashing and salting before saving
baseSchema.pre('save', function (next) {

	let base = this;
	// only hash the password if it has been modified (or is new)
	if (!base.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(base.password, salt, function (err, hash) {
	if (err) return next(err);

	// override the cleartext password with the hashed one
	base.password = hash;
	next();
		});
	});
	});
回答如下:

尝试这样的事情。确保const Base = mongoose.model('Base',baseSchema);在代码的末尾,因为它负责创建模型,因为你在预钩子之前已经将它声明在顶部,所以它不会被创建,密码也不会被哈希。

    // On Save Hook, encrypt password
    // Before saving a model, run this function
    baseSchema.pre('save', function (next) {
      //get access to the user model
      const base= this;

      // generate a salt then run callback
      bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
        if (err) { return next(err); }

        // hash (encrypt) our password using the sale
        bcrypt.hash(base.password, salt, null, function (err, hash) {
          if (err) { return next(err); }

          //overwrite plain text password with encrypted password
          base.password = hash;
          next();
        });
      });
    });

const Base = mongoose.model('Base', baseSchema);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论