Python/python3 MD5 Encryption Instance Learning

Source: Internet
Author: User
Tags md5 md5 encryption in python

Python3 for MD5 encryption

The first is to import the required modules for MD5 encryption:

Import Hashlib

Then create the MD5 object:

m = Hashlib.md5 ()

MD5 encryption by passing in a string that needs to be encrypted:

M.update ("Str4md5encode")

You can then obtain a string that has been MD5 encrypted:

Encodestr = M.hexdigest ()
Print Encodestr

The output results are:

f8fd73cf519e6f11513d505b9dd33541

Of course, for code reuse, we can write a few simple code to a function, the function input is a string that needs to be MD5 encrypted, the output is the result of MD5 encryption:

Import Hashlib
def md5encode (str):
m = Hashlib.md5 ()
M.update (str)
Return M.hexdigest ()


Above for python3.* and earlier in the way, the latest pyhon3.* call way:

def md5encode (str):
Import Hashlib
#参数必须是byte类型, otherwise reported unicode-objects must be encoded before hashing error
m = Hashlib.md5 (Str.encode (encoding= ' utf-8 '))
Return M.hexdigest ()

Print (Md5encode (' PHP Learning blog '))
# B3227D75802332FA9AFE24F7285FBAB2



Python implements MD5 encryption

The implementation of MD5 encryption in Python is done through hashlib. Before we demonstrate, we create a piece of text to encrypt the data. as follows: hashlib_data.py

#--encoding= ' Utf-8 '--

Import Hashlib

Lorem= "" "Lorem ipsum dolor sit amet consectetuer adipiscing. Donecegestas, enim et consectetuer ullamcorper, lectus-ligula rutrum leo,a elementum-elit-tortor-EU quam. Duis tincidunt nisi ut ante. Nullafacilisi. Sed tristique eros eu libero. Pellentesque Velarcu. Vivamus purus Orci, iaculis ac, suscipit sit amet, Pulvinar eu,lacus. Praesent placerat tortor sed nisl. Nunc blandit diam Egestasdui. Pellentesque habitant morbi tristique senectus et netus etmalesuada fames the AC turpis. Aliquam Viverra Fringillaleo. Nulla feugiat augue eleifend Nulla. Vivamus Mauris. Vivamus Sedmauris in Nibh placerat egestas. Suspendisse Potenti. Maurismassa. Ut eget velit auctor tortor blandit Sollicitudin. Suspendisseimperdiet Justo. """

Using MD5 for encryption is very simple, just call the MD5 encryption algorithm in HASHLIB:

#--encoding= ' Utf-8 '--

Import Hashlib from Hashlib_data

Import Lorem

H=HASHLIB.MD5 ()

H.update (Lorem)

Print H.hexdigest ()

H=HASHLIB.MD5 () is used to create MD5 cryptographic objects;

H.update (Lorem) is used to encrypt Lorem characters using the MD5 algorithm. The output results are as follows:

913fa23cc9326eb26f62859c9feed3e4

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.