hammygoonan
hammygoonan

Reputation: 2225

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.

Upvotes: 3

Views: 1847

Answers (1)

hammygoonan
hammygoonan

Reputation: 2225

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.

Upvotes: 5

Related Questions