使用python/bcrypt将密码保存为mongodb中用户集合中的含盐哈希 [英] save password as salted hash in mongodb in users collection using python/bcrypt

查看:91
本文介绍了使用python/bcrypt将密码保存为mongodb中用户集合中的含盐哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个加盐的密码哈希,并将其存储在称为用户的MongoDB集合中,如下所示:

I want to generate a salted password hash and store it in MongoDB collection called users, like this:

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

我不确定如何使用Bcrypt生成哈希密码,然后在我登录flask应用程序时,能够检查哈希值是否与存储在MongoDB中的哈希密码匹配.

I'm not sure how to generate the hashed password using Bcrypt, then when I login in my flask app, be able to check if the hash matches with the hashed password stored in MongoDB.

推荐答案

我不知道您如何使用mongodb来带来数据,但是如果您想对传递的哈希进行哈希处理,就像这样:

I don't know how you use mongodb to bring the data, but if you want to hash the pass it's as easy as:

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() 函数:

And then if you want to check the password, you can use the check_password_hash() function:

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

这篇关于使用python/bcrypt将密码保存为mongodb中用户集合中的含盐哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆