Python module hashlib

Python's hashlib provides common digest algorithms such as MD5, SHA1, and more.

What is a digest algorithm? Digest algorithm is also called hash algorithm, hash algorithm. It converts any length of data into a fixed-length data string (usually represented by a hexadecimal string) through a function.

The digest algorithm uses the digest function f() to calculate a fixed-length digest digest for data of any length, in order to find out whether the original data has been tampered with.

The reason why the digest algorithm can indicate whether the data has been tampered with is because the digest function is a one-way function, and it is easy to calculate f(data), but it is very difficult to reverse the data through digest. Moreover, making a bit modification to the original data will cause the calculated digest to be completely different.

MD5 is the most common digest algorithm, which is very fast and generates a fixed 128 bit byte, usually represented by a 32-bit hexadecimal string.

The result of SHA1 is a 160-bit byte, usually represented by a 40-bit hexadecimal string. More secure algorithms than SHA1 are SHA256 and SHA512, but the more secure algorithms are slower and have longer digest lengths.

Let's take the digest algorithm MD5, SHA1 as an example:

import hashlib

md5 = hashlib.md5()
md5.update('esrdtfhij'.encode('utf-8'))
print(md5.hexdigest())  # b853c213f701e054bca65bb96fb461eb

sha = hashlib.sha1()
sha.update('esrdtfhij'.encode('utf-8'))
print(sha.hexdigest())  # 6f0b74185f30b1e1cf8e4ba355d9774ab4671295

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970518&siteId=291194637