将纯文本和哈希值与 bcrypt 进行比较时出现无效盐错误

发布于 2025-01-17 04:06:02 字数 655 浏览 2 评论 0原文

我正在尝试使用 bcrypt 比较 python 中保存的哈希值和用户输入。我的代码:

while passnotcorrect == True:
            password = input("Enter password: ")
            password = password.encode('utf-8')
            file = open('password.txt', 'r')
            checkhash = file.read()
            file.close()
            checkhash = checkhash.encode('utf-8')
            if bcrypt.checkpw(password, checkhash):
                passnotcorrect = False
                os.system('cls||clear')
            else:
                print("Password is incorrect \n")

错误:

ValueError:盐无效

我真的很需要一些帮助。我不确定为什么这个函数首先需要盐。谢谢

I'm trying to compare a saved hash and a user input in python using bcrypt. My code:

while passnotcorrect == True:
            password = input("Enter password: ")
            password = password.encode('utf-8')
            file = open('password.txt', 'r')
            checkhash = file.read()
            file.close()
            checkhash = checkhash.encode('utf-8')
            if bcrypt.checkpw(password, checkhash):
                passnotcorrect = False
                os.system('cls||clear')
            else:
                print("Password is incorrect \n")

The error:

ValueError: Invalid salt

I'd really like some help with this. I'm not sure why this function would require the salt in the first place. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2025-01-24 04:06:03

有点晚了,但我认为你的问题是你试图将 utf8 编码的字符串输入“password”与从文件读取的另一个字符串“checkhash”进行比较。

Bcrypt.checkpw() 接受要检查的密码的 UTF8 编码字符串作为第一个参数,后跟 UTF8 编码的 hash 来比较所提供的密码,以查看提供的密码是否与哈希密码匹配。

太长了;当第二个参数需要是您要比较的哈希值(黄金标准)时,您将两个字符串传递给 checkpw 方法。

db_pass = "pw-string123"  # from database
password = "pw-string123"  # from input
db_hashed_pass = bcrypt.hashpw(db_pass.encode('utf8'), bcrypt.gensalt())
print(f'Hashed pass to save in db: {db_hashed_pass}')
is_pass_matching = bcrypt.checkpw(password.encode('utf8'), db_hashed_pass)

print(f'Do passwords match? {is_pass_matching}')

确保您的哈希密码在输入或检索时不会被截断(长度匹配)。

A little late but I think your issue is that you're trying to compare 'password' which is utf8 encoded string input with 'checkhash', another string read from a file.

Bcrypt.checkpw() takes in a UTF8 encoded string for the password to check as the first argument followed by the UTF8 encoded hash to compare the password being provided against to see if the provided pass matches the hash pass.

TLDR; you're passing two strings to the checkpw method, when the second argument needs to be the hash you're comparing against (gold standard).

db_pass = "pw-string123"  # from database
password = "pw-string123"  # from input
db_hashed_pass = bcrypt.hashpw(db_pass.encode('utf8'), bcrypt.gensalt())
print(f'Hashed pass to save in db: {db_hashed_pass}')
is_pass_matching = bcrypt.checkpw(password.encode('utf8'), db_hashed_pass)

print(f'Do passwords match? {is_pass_matching}')

Make sure your hashed password isn't being truncated (length matches) upon entry or retrieval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文