bcrypt.checkpw returns TypeError: Unicode-objects must be encoded before checking

前端 未结 4 1846
有刺的猬
有刺的猬 2021-02-05 05:39

I am calling bcrypt.checkpw to check unencrypted password matches with hashed password stored in the credential database, but receive

TypeEr

4条回答
  •  失恋的感觉
    2021-02-05 06:24

    i use something like that

    class User(Base):
        __tablename__ = "user"
        id = Column(BigInteger, primary_key=True, autoincrement=True)
    
        login = Column(String, nullable=False, unique=True)
        password = Column(String, nullable=False)
    
        @staticmethod
        def make_password_hash(password):
            hash = bcrypt.hashpw(password=password.encode('utf-8'), salt=bcrypt.gensalt())
            return hash.decode('utf-8')
    
        def is_password_valid(self, password):
            return bcrypt.checkpw(password.encode('utf-8'), self.password.encode('utf-8'))
    

提交回复
热议问题