简体   繁体   English

如何将散列密码与 Python 中的 Input() 进行比较?

[英]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.

相关问题 如何使用 bcrypt 将纯文本密码与散列密码进行比较? - How to compare plain text password to hashed password using bcrypt? 如何在 Django 上存储散列密码以及如何与另一个散列密码进行比较? - How do I store a hashed password on Django and how do i compare with another hashed password? 如何在Python中验证散列(sha1)密码? - How to authenticate hashed(sha1) password in Python? 如何从 Python 中的数据库中减去哈希密码 - How to substract a hashed password from database in Python 如何比较来自数据库的散列密码和输入到使用 tkinter 制作的 GUI 登录系统的登录密码 - How to compare hashed password from db and login password entered into GUI login system made with tkinter Python中的哈希密码验证不起作用 - Hashed password authentication in Python not working 在python中恢复盐腌和哈希密码 - Recover a salted and hashed password in python 搜索哈希密码文件Python - Search Hashed Password File Python 如何在python(django)中hash字符串并将散列值与给定字符串进行比较 - How to hash strings in python(django) and compare the hashed value with a given string 如何使用bcrypt比较在python中存储为字符串的哈希密码? - How to compare hashed passwords stored as strings in python using bcrypt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM