Python Hashlib

In this tutorial, we are going to learn about Python Hashlib module. We can use python hashlib module to generate message digest or secure hash from the source message. In our previous tutorial we learnt about Python Math.

在本教程中,我们将学习Python Hashlib模块。 我们可以使用python hashlib模块从源消息生成消息摘要或安全哈希。 在上一教程中,我们了解了Python Math 。

Python Hashlib (Python Hashlib)

For generating python secure hash message, we need to use hashlib module.

为了生成python安全哈希消息,我们需要使用hashlib模块。

Python hashlib hashing function takes variable length of bytes and converts it into a fixed length sequence. This is a one way function. That means, you hash a message, you get a fixed length sequence. But you cannot get the original message from those fixed length sequence.

Python hashlib哈希函数采用可变长度的字节,并将其转换为固定长度的序列。 这是一种单向功能。 这意味着,您对消息进行哈希处理,将获得固定长度的序列。 但是您无法从这些固定长度的序列中获取原始消息。

In cryptography, a hash algorithm is considered to be better if the original message cannot be decrypted from the hash message. Also changing one byte in the original message makes significant change in the message digest value.

在密码术中,如果无法从哈希消息中解密原始消息,则认为哈希算法更好。 同样,更改原始消息中的一个字节会使消息摘要值发生重大变化。

Python secure hash values are used in storing password in encrypted form. So even the application owner won’t have access to user password, passwords are matched when user enters the password again and hash value is calculated and compared with the stored value.

Python安全哈希值用于以加密形式存储密码。 因此,即使应用程序所有者也无权访问用户密码,当用户再次输入密码并计算哈希值并将其与存储的值进行比较时,密码也会匹配。

可用的散列算法 (Available Hashing Algorithms)

We can use algorithms_available function to get the list of all the algorithms available in the system, including those available through OpenSSl. Duplicate algorithm names can be seen also.

我们可以使用algorithms_available函数来获取系统中所有可用算法的列表,包括通过OpenSSl可用的算法。 也可以看到重复的算法名称。

Again, by using algorithms_guaranteed function you can see the algorithms present in the module. See the following code.

同样,通过使用algorithms_guaranteed函数,您可以看到模块中存在的算法。 请参阅以下代码。

import hashlib

print(hashlib.algorithms_available)
print(hashlib.algorithms_guaranteed)

For my system, the output was like this below. It can differ based on the operating system configurations.

对于我的系统,输出如下所示。 它可以根据操作系统配置而有所不同。

{'MD5', 'SHA', 'sha1', 'shake_256', 'sha3_384', 'DSA-SHA', 'MD4', 'sha3_224', 'SHA1', 'ripemd160', 'SHA512', 'sha224', 'whirlpool', 'RIPEMD160', 'DSA', 'blake2s', 'SHA384', 'ecdsa-with-SHA1', 'md5', 'blake2b', 'shake_128', 'dsaEncryption', 'sha', 'sha256', 'md4', 'SHA224', 'SHA256', 'sha3_256', 'sha512', 'dsaWithSHA', 'sha384', 'sha3_512'}
{'sha256', 'blake2s', 'sha3_224', 'sha224', 'sha1', 'md5', 'sha3_256', 'shake_256', 'sha512', 'blake2b', 'shake_128', 'sha3_384', 'sha384', 'sha3_512'}

Python hashlib示例 (Python hashlib example)

To use Python hashlib module, you just have to know few functions. By using hashlib.encryption_algorithm_name(b"message") function, you can hash the whole message at once.

要使用Python hashlib模块,您只需要了解一些功能。 通过使用hashlib.encryption_algorithm_name(b"message")函数,您可以一次对整个消息进行哈希处理。

Also, you can use the update() function to append byte message to the secure hash value. In both case, the output will be same. Finally, by using digest() function you can get the secure hash.

另外,您可以使用update()函数将字节消息附加到安全哈希值。 在两种情况下,输出都是相同的。 最后,通过使用digest()函数,您可以获得安全的哈希。

Note that, b is written in the left of the message to be hashed. This b indicates that that string is a byte string. Let’s look at the hashlib example for more clarity.

注意, b写入要散列的消息的左侧。 此b表示该字符串是字节字符串。 让我们看一下hashlib示例以更清楚。

import hashlib  # import hashlib module

# initialize using sha256
print('\nExample for SHA256')
m = hashlib.sha256()
# append string one after another
m.update(b"This is a")
m.update(b" great python tutorial.")
print('Output 1 :', m.digest())

# use the whole string at once
x = hashlib.sha256(b"This is a great python tutorial.")
print('Output 2 :', x.digest())

# initialize using md5
print('\nExample for md5')
m = hashlib.md5()
# append string one after another
m.update(b"This is a")
m.update(b" great python tutorial.")
print('Output 1 :', m.digest())

# use the whole string at once
x = hashlib.md5(b"This is a great python tutorial.")
print('Output 2 :', x.digest())

So the output will be

所以输出将是

So, this is all about Python hashlib module. Hope that you learned well. See the official reference to learn more about it.

因此,这一切都与Python hashlib模块有关。 希望你学得好。 有关更多信息,请参见官方参考 。

翻译自: https://www.journaldev.com/16035/python-hashlib

你可能感兴趣的:(Python Hashlib)