簡體   English   中英

使用python / bcrypt將密碼保存為用戶集合中mongodb中的salted哈希

[英]save password as salted hash in mongodb in users collection using python/bcrypt

我想生成一個salted密碼哈希並將其存儲在名為users的MongoDB集合中,如下所示:

users_doc = { 
    "username": "James",
    "password": "<salted_hash_password>"
}

我不確定如何使用Bcrypt生成散列密碼,然后當我登錄我的燒瓶應用程序時,能夠檢查散列是否與存儲在MongoDB中的散列密碼匹配。

我不知道你是如何使用mongodb來傳輸數據的,但是如果你想對傳遞進行散列,那么就像以下一樣簡單:

from flask import Flask
from flask.ext.bcrypt import Bcrypt

app = Flask(__name__)
bcrypt = Bcrypt(app)

# Your code here...

users_doc = {
    "username": "james",
    "password": bcrypt.generate_password_hash(password)
}

然后,如果要檢查密碼,可以使用check_password_hash()函數:

bcrypt.check_password_hash(users_doc["password"], request.form["password"]) # Just an example of how you could use it.

使用bcrypt生成salt並將其保存在設置文件中:

import bcrypt
salt = bcrypt.gensalt()

要加密密碼:

password = "userpassword"
hashed = bcrypt.hashpw(password, bcrypt.gensalt())

檢查生成的鹽:

>>> print hashed
$2a$12$C.zbaAxJPVVPKuS.ZvNQiOTVSdOf18kMP4qDKDnM3AGrNyGO5/tTy

要檢查給定的密碼是否與您生成的密碼匹配(只需使用salt創建密碼的哈希值並將其與數據庫上的密碼進行比較):

given_password = "password"
hashed_password = bcrypt.hashpw(password, salt) #Using the same salt used to hash passwords on your settings

hashed_password == hashed #In this case it returns false, because passwords are not the same

您可以使用以下哈希密碼。

app.post("/register", function(req, res){
    var type = req.body.type
    var newUser = new Student({
        username: req.body.username,
        gender: req.body.gender,
        rollnumber: req.body.rollnumber,
        dob: req.body.dob,
        email: req.body.email,
        type: req.body.type,
        password: req.body.password
    })

    req.checkBody('username','UserName is Required').notEmpty();
    req.checkBody('rollnumber','Roll Number is Required').notEmpty();
    req.checkBody('email','Email Required').notEmpty();
    req.checkBody('email','Email Invalid').isEmail();
    req.checkBody('password','Password is Required').notEmpty();
    req.checkBody('password1','Passwords do not match').equals(req.body.password);

    var errors = req.validationErrors();
    if(errors){
        res.render('Sregister', {errors: errors});
    }else{
    bcrypt.genSalt(10, function(err,  salt){
        bcrypt.hash(newUser.password, salt, function(err, hash){
            if(!err){
                newUser.password = hash;
            }
            newUser.save(function(err){
                if(!err){
                    console.log("success in reg");
                    res.redirect("/student/login")
                }
            })
        })
    })

並在登錄時使用以下內容來比較密碼。

passport.use('student', new LocalStrategy(function(username, password, done){
    var query = {username: username};
    Student.findOne(query, function(err, student){
        if(err) throw err;
        if(!student){
            return done(null, false);
        }
        bcrypt.compare(password,student.password, function(err, isMatch){
            if(err) throw err;
            if(isMatch)
                return done(null, student);
            else
                return done(null,false);
        })
    })
}))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM