Python bcrypt postgres encoding / decoding issue?

Viewed 666

I have a simple Flask App that uses bcrypt. I have spent several hours on this simple easy to use library to figure out the encoding issue. The login method calls doDecideHashedPassword method which then should return true or false if the credentials match.

Also note the following

  1. I use postgres and currently my password field is set to varchar(500)
  2. I convert the hashed password to str to store it accurately in database.

When I request the hashed password from the database, and pass it through the doDecodeHashedPassword below, I get invalid salt.This accepts email and password from the form.

def doDecodeHashedPassword(self, email, password):
        getpwd = ClientUser.query.filter_by(email=email).one()
        getpwd = getpwd.password
        getpwd = getpwd.encode()
        password = password
        check_user_integrity = bcrypt.checkpw(password,getpwd)
        if check_user_integrity:
            return (check_user_integrity)
        else:
            return False

Why am I getting invalid salt error.

1 Answers

All i needed to do was ensure that the decoded format of the password is stored in the database. This drove me crazy for a while, but now it is resoved.

bcrypt.hashpw(password, bcrypt.gensalt(16)).decode()

The fundamental problem is that you need to ensure you are not storing the hashed password in the DB as varchar as the string may be encoded automatically during the ETL process. Instead, just use plain "TEXT" field or "CHAR" field instead. Ideal length should be around 80 characters.I just had 100 for safety sake.

Related