如何在 Python 中使用 sha256 哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26539366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:37:10  来源:igfitidea点击:

How to use sha256 hash in Python

pythonsha256

提问by user3479683

I am trying to read in a file of passwords. Then I am trying to compute the hash for each password and compare it to a hash I already have to determine if I have discovered the password. However the error message I keep getting is "TypeError: Unicode-objects must be encoded before hashing". Here is my code:

我正在尝试读取密码文件。然后我试图计算每个密码的哈希值,并将其与我已经必须确定的哈希值进行比较,以确定我是否发现了密码。但是,我不断收到的错误消息是“TypeError: Unicode-objects must be encoding before hashing”。这是我的代码:

from hashlib import sha256

with open('words','r') as f:
    for line in f:

        hashedWord = sha256(line.rstrip()).hexdigest()

        if hashedWord == 'ca52258a43795ab5c89513f9984b8f3d3d0aa61fb7792ecefe8d90010ee39f2':
            print(line + "is one of the words!")

Can anyone please help and provide an explanation?

任何人都可以请帮忙并提供解释吗?

采纳答案by abarnert

The error message means exactly what it says: You have a Unicode string. You can't SHA-256-hash a Unicode string, you can only hash bytes.

错误消息的含义正是它所说的:您有一个 Unicode 字符串。您不能对 Unicode 字符串进行 SHA-256 散列,只能对字节进行散列。

But why do you have a Unicode string? Because you're opening a file in text mode, which means you're implicitly asking Python to decode the bytes in that file (using your default encoding) to Unicode. If you want to get the raw bytes, you have to use binary mode.

但是为什么你有一个 Unicode 字符串?因为您在文本模式下打开文件,这意味着您隐含地要求 Python 将该文件中的字节(使用您的默认编码)解码为 Unicode。如果要获取原始字节,则必须使用二进制模式。

In other words, just change this line:

换句话说,只需更改此行:

with open('words','r') as f:

… to:

… 到:

with open('words', 'rb') as f:


You may notice that, once you fix this, the printline raises an exception. Why? because you're trying to add a bytesto a str. You're also missing a space, and you're printing the un-stripped line. You could fix all of those by using two arguments to print(as in print(line.rstrip(), "is one of the words")).

您可能会注意到,一旦修复此问题,该print行就会引发异常。为什么?因为您正在尝试将 a 添加bytes到 a str。您还缺少一个空格,并且您正在打印未剥离的行。您可以通过使用两个参数来解决所有这些问题print(如print(line.rstrip(), "is one of the words"))。

But then you'll get output like b'\xc3\x85rhus' is one of the wordswhen you wanted it to print out ?rhus is one of the words. That's because you now have bytes, not strings. Since Python is no longer decoding for you, you'll need to do that manually. To use the same default encoding that sometimes works when you don't specify an encoding to open, just call decodewithout an argument. So:

但是你会得到像b'\xc3\x85rhus' is one of the words你想要它打印时一样的输出?rhus is one of the words。那是因为您现在拥有字节,而不是字符串。由于 Python 不再为您解码,您需要手动进行解码。要使用在您未指定编码时有时有效的相同默认编码open,只需在decode不带参数的情况下调用。所以:

print(line.rstrip().decode(), "is one of the words")