Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bcrypt Invalid Salt and Postgresql

I have a little Flask app (Python3) that is starting to get a little more complicated so I decided to switch from SQLite to Postgresql. I knew this would throw a few issues for me but my codebase has decent test coverage so I was confident I could iron out any wrinkles before pushing to production.

In particular, anywhere I entered a password, I had to encode (password.encode('utf-8')).

(As an aside, timezones was the other area I had some issues. I ultimately ended up removing timezones from any date I was using.)

But there's just one last bug I can't figure out. To test that passwords are updated I have the following test:

self.assertTrue(bcrypt.check_password_hash(
    user.password, new_password
))

That should check the current password (which looks like a bytes sting when I print it) against the new_password. But I get an error saying ValueError: Invalid salt

I'd love to know how to fix this but I'd also love someone to explain what's going on here.

like image 244
hammygoonan Avatar asked Aug 23 '26 10:08

hammygoonan


1 Answers

So it turns out the problem was the way I was saving the password. In this particular instance I should have saved the password like so:

user.password = bcrypt.generate_password_hash(
        request.form['password']
).decode('utf-8')

db.session.commit()

Now the test above works.

like image 159
hammygoonan Avatar answered Aug 26 '26 09:08

hammygoonan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!