为什么我需要在 Python 中散列之前声明编码,我该怎么做



我正在尝试创建一个类似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,%sn" % (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)并找到了所需的语法,添加了它,我仍然收到相同的结果。

哈希适用于字节str对象包含 Unicode 文本,而不是字节,因此必须先进行编码。选择一种编码,a) 可以处理您可能遇到的所有代码点,也许 b) 生成相同哈希的其他系统也使用。

如果你是哈希的唯一用户,那么只需选择UTF-8;它可以处理所有的Unicode,并且对西方文本最有效:

newpassword = hashlib.sha224(newpassword.encode('utf8')).hexdigest()

hash.hexdigest()的返回值是 Unicode str值,因此您可以安全地将其与从文件中读取的str值进行比较。

最新更新