An explanation of MD5 encryption usage for Python

Source: Internet
Author: User
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 ())

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.