How Python implements MD5 encryption

The gods are silent-personal CSDN blog post directory

This article describes how to use Python's hashlib library to implement MD5 encryption on strings.

Official documentation of the hashlib library: hashlib — Secure hashes and message digests — Python 3.11.3 documentation
The hashlib library comes with Python 3, so no additional installation steps are required.

Assuming that the string to be encrypted is 'apple', the code for the encryption process is:

import hashlib

str1='apple'
md=hashlib.md5()     #获取一个md5加密算法对象
md.update(str1.encode('utf-8'))
sign=md.hexdigest()
print(sign)

The last printed sign ( 1f3870be274f6c49b3e31a0c6728957f) is the result of MD5 encryption.

Other references used in writing this article:

  1. Python calculates the md5 value of the file - have a good laugh - 博客园
  2. python - How to correct TypeError: Unicode-objects must be encoded before hashing? - Stack Overflow
  3. Python learning - hashlib.md5 summary algorithm (hash algorithm) - geerniya's blog - CSDN blog

Guess you like

Origin blog.csdn.net/PolarisRisingWar/article/details/130967457