【发布时间】:2016-12-22 12:48:02
【问题描述】:
我正在尝试在我的应用中使用散列密码,如下所示:
class UserService():
def register_user(self, username, email, password):
if self.__checkIfUserExists(username) is True:
return False
else:
hashed_password = self.__hash_password(password.encode('utf-8'), bcrypt.gensalt())
user = User(username=username, email=email, password_hash=hashed_password)
db.session.add(user)
db.session.commit()
db.session.close()
return True
def authenticate_user(self, email, password):
user = User.query.filter_by(email=email).first()
hashed_pw = self.__hash_password(password.encode("utf-8"), bcrypt.gensalt())
print(hashed_pw == user.password_hash)
if user and hashed_pw == user.password_hash:
return True
return False
def __checkIfUserExists(self, username):
exists = db.session.query(db.exists().where(User.username== username)).scalar()
return exists
def __hash_password(self, password, salt):
return bcrypt.hashpw(password, salt)
嗯,密码永远不会匹配。
如何让它工作?我的错误在哪里?我以为我必须将提供的密码的哈希值与存储在数据库中的哈希值进行比较..?
【问题讨论】: