From d7a218a21240ab744288b4b6866452bf0dfe9d26 Mon Sep 17 00:00:00 2001 From: Felix Kaechele Date: Sat, 17 Sep 2016 07:25:05 -0700 Subject: [PATCH] Ensure correct encoding when hashing and verifying Depending on the database backend the string might not be UTF-8 encoded. This makes sure that the hashing function works regardless of that. Signed-off-by: Felix Kaechele --- app/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models.py b/app/models.py index 942cf51..7f14d40 100644 --- a/app/models.py +++ b/app/models.py @@ -118,11 +118,11 @@ class User(db.Model): # Hash a password for the first time # (Using bcrypt, the salt is saved into the hash itself) pw = plain_text_password if plain_text_password else self.plain_text_password - return bcrypt.hashpw(pw, bcrypt.gensalt()) + return bcrypt.hashpw(pw.encode('utf-8'), bcrypt.gensalt()) def check_password(self, hashed_password): # Check hased password. Useing bcrypt, the salt is saved into the hash itself - return bcrypt.checkpw(self.plain_text_password, hashed_password) + return bcrypt.checkpw(self.plain_text_password.encode('utf-8'), hashed_password.encode('utf-8')) def get_user_info_by_id(self): user_info = User.query.get(int(self.id))