BCrypt. How to store salt with python3?

Viewed 7756

We have code, that work for python 2.

@password.setter
def password(self, value):
    self.salt = bcrypt.gensalt()
    self.passwd = bcrypt.hashpw(value.encode('utf-8'), self.salt)

def check_password(self, value):
    return bcrypt.hashpw(value.encode('utf-8'), self.salt.encode('utf-8')) == self.passwd

However, when I try to convert it to python3, we meet following problems:

Error one happens on cassandra driver level:

cassandra.cqlengine.ValidationError: passwd <class 'bytes'> is not a string

Ok. Casting salt and passwd to string:

@password.setter
def password(self, value):
    salt = bcrypt.gensalt()
    self.salt = str(salt)
    self.passwd = str(bcrypt.hashpw(value.encode('utf-8'), salt))

Now salt saves. But in check_password we get ValueError: Invalid salt. If we change check password code to:

def check_password(self, value):
    return bcrypt.hashpw(value, self.salt) == self.passwd

We get error TypeError: Unicode-objects must be encoded before hashing.

Where to dig?

UPD Salt values in password and check password look same, for example:

b'$2b$12$cb03angGsu91KLj7xoh3Zu'                                                                        
b'$2b$12$cb03angGsu91KLj7xoh3Zu'
1 Answers
Related