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>"
}
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.
Generate a salt using bcrypt and keep it saved in your settings file:
To encrypt the password:
Checking the generated salt:
To check if a given password matches the one you generated (just create a hash of the password using the salt and compare it to the one on the database):
You can use the following the hash your password.
And use the following to compare the password while logging in.
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:
And then if you want to check the password, you can use the
check_password_hash()
function: