【发布时间】:2023-11-23 14:36:01
【问题描述】:
我试图从文件中散列密码,然后将其与用户提供的密码匹配。并不是说超级安全只是足够安全,这样密码就不是文件中的纯文本。 我得到错误
TypeError: Unicode-objects must be encoded before hashing
如果我输入 hashpass = hashlib.md5(b'p').hexdigest() 就可以了 但它只加密“p”
如何让它加密我的字符串输入?
程序
import hashlib
status = ""
def passhash():
code = open("password.txt", "r")
password = code.readline()
global encrypt
encrypt = hashlib.md5(password).hexdigest()
def checkPassword():
for key in range(3):
p = input("Enter the password >>")
hashpass = hashlib.md5(p).hexdigest()
if hashpass == encrypt:
print("password correct!")
status = "q"
return status
else:
print ('wrong password, try again')
print ('you have failed')
def Main():
status = input("p for program, q to quit: ")
if status == "p":
passhash()
checkPassword()
elif status == "q":
print("Byebye")
status = "q"
return status
while status != "q":
status = Main()}
【问题讨论】:
-
嗯,您是否按照错误消息的提示进行操作并尝试encoding
password? -
您不想将 md5 用于任何密码散列,它不是为它设计的。相反,您想查看
passlibmodule,或在 unix 平台上使用cryptmodule。它们的使用并不比您幼稚的方法复杂。
标签: python encryption login passwords md5