我正在尝试创建一个类似 AI 的聊天机器人,它的功能之一是登录。我以前使用过登录代码,它工作正常,但我现在在处理密码散列的代码时遇到了困难。这是代码:
import hashlib
...
register = input ("Are you a new user? (y/n) >")
password_file = 'passwords.txt'
if register.lower() == "y":
newusername = input ("What do you want your username to be? >")
newpassword = input ("What do you want your password to be? >")
newpassword = hashlib.sha224(newpassword).hexdigest()
file = open(password_file, "a")
file.write("%s,%s\n" % (newusername, newpassword))
file.close()
elif register.lower() == ("n"):
username = input ("What is your username? >")
password = input ("What is your password? >")
password = hashlib.sha224(password).hexdigest()
print ("Loading...")
with open(password_file) as f:
for line in f:
real_username, real_password = line.strip('\n').split(',')
if username == real_username and password == real_password:
success = True
print ("Login successful!")
#Put stuff here! KKC
if not success:
print("Incorrect login details.")
这是我得到的结果:
Traceback (most recent call last):
File "<FOLDERSYSTEM>/main.py", line 36, in <module>
newpassword = hashlib.sha224(newpassword).hexdigest()
TypeError: Unicode-objects must be encoded before hashing
我查找了我认为应该使用的编码 (latin-1) 并找到了所需的语法,将其添加进去,我仍然收到相同的结果。