ValueError: Invalid salt



我已经使用此代码编码解码用户密码,以便使用SQLAlchemy保存在SQL数据库PostgreSQL中。

在登录过程中(使用verify_password函数(,我将获得ValueError:Invalid salt

import bcrypt

def encode_password(password: str) -> bytes:
"""
Hashing the Password
"""
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())

def verify_password(password: str, hashed_password: str):
"""
Decode the Password
"""
return bcrypt.checkpw(password.encode("utf-8"), hashed_password.encode("utf-8"))

用户模型

class User(Base):
"""
User Model
"""
__tablename__ = "users"
id = Column(Integer, primary_key=True)
mobile = Column(String, unique=True, nullable=False)
email = Column(String, unique=True, nullable=False)
password = Column(String, nullable=False)

我发现有问题

在保存过程中,我们应该对hashed-password进行解码,以便将其保存到数据库中。像这样:

def create_user(db: Session, user: schemas.UserRegister):
"""
Register A New User
"""
hashed_password = encode_password(
user.password
)
db_user = models.User(
mobile=user.mobile,
email=user.email,
password=hashed_password.decode("utf-8")
)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user

最新更新