I make the assumption that you are using Python 3. With Python 3, strings are by default unicode strings.
If you call the bcrypt.checkpw()
function with unicode values:
import bcrypt password = "seCr3t" # unicode string hashed_password = "hashed_seCr3t" # unicode string bcrypt.checkpw(password, hashed_password)
You will get this exception
Traceback (most recent call last): ... TypeError: Unicode-objects must be encoded before checking
The reason is simple: cryptographic functions work only with byte strings (or arrays).
Password and hashed_password should be as byte strings.
If you use the bcrypt.hashpw()
function, your hashed_password should be a string of bytes, and I think the problem is the password value. This password should come from an HTML form of something similar. To use the bcrypt.checkpw()
function, you must first encode the string value using the same encoding that you use to encrypt the password with the bcrypt.hashpw()
function. Usually we choose the encoding "utf8".
For example (Python 2 and 3):
import bcrypt # at creation first: password = u"seCr3t" hashed_password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt()) # first attempt: password = u"seCrEt" bcrypt.checkpw(password.encode('utf8'), hashed_password) # -> False # second attempt: password = u"seCr3t" bcrypt.checkpw(password.encode('utf8'), hashed_password) # -> True
See easy use on the Gihub page
source share