2

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

4

1 回答 1

2

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

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

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

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

于 2016-07-11T10:02:03.663 回答