哈希函数——示例
In this section we shall provide a few examples about calculating cryptographic hash functions in Python.
在本节中,我们将提供一些有关在 Python 中使用加密哈希函数计算的示例。
在 Python 中使用加密哈希函数计算
We shall use the standard Python library hashlib
. The input data for hashing should be given as bytes sequence (bytes object), so we need to encode the input string using some text encoding, e.g. utf8
. The produced output data is also a bytes sequence, which can be printed as hex digits using binascii.hexlify()
as shown below:
我们将使用标准的 Python 库 hashlib
。用于哈希的输入数据应该以字节序列(bytes 对象)的形式给出,因此我们需要使用一些文本编码例如 utf8
对输入字符串进行编码。产生的输出数据也是一个字节序列,可以使用 binascii.hexlify()
打印为十六进制数字,如下所示:
The expected output from the above example looks like this:
上述示例的预期输出如下:
Now write some Python code to calculate a Keccak-256 hash:
现在编写一些 Python 代码来计算一个 Keccak-256 的哈希值:
The output from the above examples is:
上述示例的预期输出为:
Last updated
Was this helpful?