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

前端 未结 4 1849
有刺的猬
有刺的猬 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:06

    Using that like e.g. and see the comments below the code:

    import bcrypt
    
    def comparacaoSenha(passw, hash):
        if bcrypt.checkpw(passw, hash):
            print("It Matches!")
        else:
                print("It Does not Match")
    
    password = "super secret password".encode('utf-8')
    password2 = "another password".encode('utf-8')
    
    hashed = bcrypt.hashpw(password, bcrypt.gensalt())
    heashed2 = bcrypt.hashpw(password2, bcrypt.gensalt())
    
    print("Teste 1: ")
    print("-----------------------------")
    print("PASSWORD: ", password)
    print("HASHED PW: ", hashed)
    comparacaoSenha(password, hashed)
    print("-----------------------------")
    
    print("Teste 2: ")
    print("-----------------------------")
    print("PASSWORD: ", password2)
    print("HASHED PW: ", heashed2)
    comparacaoSenha(password2, heashed2)
    print("-----------------------------")
    
    # Things to remember:
    # always use the encode('utf-8') or put a prefix 'b'' just before the strings
    # EX: newPass = b"OTHER PASSWORD" // or newPass="OTHER PASSWORD".encode('utf-8')
    # 
    

提交回复
热议问题