Home >Backend Development >Python Tutorial >Detailed explanation of the usage of MD5 encryption in Python
This article mainly introduces relevant information about the detailed explanation of Python MD5 encryption examples. Here are the implementation methods and examples. Friends in need can refer to the following
Detailed explanation of Python MD5 encryption
MD5 encryption under Python 3
# 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'this is a md5 test.' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为hl.update(str) 报错为: Unicode-objects must be encoded before hashing hl.update(str.encode(encoding='utf-8')) print('MD5加密前为 :' + str) print('MD5加密后为 :' + hl.hexdigest())
Running results
Encapsulation of MD5 encryption under Python3
# 生成MD5 def genearteMD5(str): # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 否则报错为:hl.update(str) Unicode-objects must be encoded before hashing hl.update(str.encode(encoding='utf-8')) print('MD5加密前为 :' + str) print('MD5加密后为 :' + hl.hexdigest())
The Python2 version has the MD5 module to generate MD5 as follows
import md5 src = 'this is a md5 test.' m1 = md5.new() m1.update(src.encode(encoding='utf-8')) print(m1.hexdigest())
The above is the detailed content of Detailed explanation of the usage of MD5 encryption in Python. For more information, please follow other related articles on the PHP Chinese website!