[英]How to compare hashed password to Input() in Python?
Goal:目标:
Currently learning how to encrypt passwords using Python.目前正在学习如何使用 Python 加密密码。
Current code:当前代码:
import bcrypt
passwd = b'False'
salt = bcrypt.gensalt()
hashed = bcrypt.hashpw(passwd, salt)
x = input()
print(hashed == bcrypt.hashpw(x, hashed))
Problem:问题:
I might be doing this wrong, need a bit of guidance how to achieve this correctly.我可能做错了,需要一些指导如何正确实现这一点。
How can I insert a input value between the apostrophes for b''
?如何在
b''
的撇号之间插入输入值? That's where the password will be and compared to the Hashed one.这就是密码所在的位置,并与哈希密码进行比较。
This is what I have tried (Also, Im I on the right lines?):这是我尝试过的(另外,我在正确的路线上?):
x = b'%s' % (input())
Output on CMD CMD 输出
x = b'%s' % (input())
TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'
Goal目标
I am trying to compare input to the hashed password.我正在尝试将输入与散列密码进行比较。
You should understand what b''
does before using it, and that would allow you to look for a solution in the right direction.在使用
b''
之前,您应该了解它的作用,这将使您能够在正确的方向上寻找解决方案。
The answer is: it indicates that what you're assigning is a string bytes literal.答案是:它表明您分配的是字符串字节文字。 That basically means that you have a bunch of bytes as your data.
这基本上意味着你有一堆字节作为你的数据。 (See these answers )
(见这些答案)
Once you know that you can search how to convert a string to bytes in Python, which leads you here一旦你知道你可以搜索如何在 Python 中将字符串转换为字节,这将引导你到这里
So, the answer to your initial question (and yes, you should use checkpw
), is:因此,您最初的问题的答案(是的,您应该使用
checkpw
)是:
print(hashed == bcrypt.hashpw(x.encode('utf-8'), hashed))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.