This article mainly introduces the Python MD5 encryption instance detailed information, here provides the realization method and the example, needs the friend can refer to the next
Detailed Python MD5 encryption
Python 3 under MD5 encryption
# because the MD5 module was removed in Python3 # MD5 operation with Hashlib module in Python3 import hashlib# information to be encrypted str = ' This is a MD5 test. ' # Create MD5 Object HL = hashlib.md5 () # tips# Here you must declare encode# if the notation is hl.update (str) Error: Unicode-objects must be encoded before HASHINGHL. Update (Str.encode (encoding= ' utf-8 ')) print (' MD5 before encryption: ' + str ') print (' MD5 encrypted after: ' + hl.hexdigest ())
Run results
MD5 Encryption under Package Python3
# Build Md5def genearteMD5 (str): # Create MD5 object hl = hashlib.md5 () # Tips # Here must declare encode # Otherwise the error is: Hl.update ( STR) unicode-objects must be encoded before hashing hl.update (Str.encode (encoding= ' utf-8 ')) print (' MD5 before encryption: ' + str ' print (' MD5 after encryption: ' + hl.hexdigest ())
Python2 version with MD5 module generated MD5 the following
Import MD5SRC = ' This is a MD5 test. ' M1 = Md5.new () m1.update (Src.encode (encoding= ' utf-8 ')) print (M1.hexdigest ())