teaches you to use Python to get the MD5 value of a string

Source: Internet
Author: User
Tags sha1 python script
You recently need to use Python to get the MD5 value of a string.

Today put the code to share with you.

  #!/usr/bin/env python        #-*-coding:cp936-*-        import hashlib           def get_md5_value (SRC):           myMd5 = HASHLIB.MD5 ()            mymd5.update (src)            mymd5_digest = Mymd5.hexdigest ()            return mymd5_digest           def get_sha1_ Value (SRC):            mySha1 = HASHLIB.SHA1 ()            mysha1.update (src)            mysha1_digest = Mysha1.hexdigest ()            Return mysha1_digest            if __name__== ' __main__ ':            src = ' aaa '            result_md5_value=get_md5_value (SRC)            Result_sha1_value=get_sha1_value (SRC)            print ' source string: ', src            print ' MD5: ', Result_md5_value            print ' SHA1: ', Result_sha1_value

Python is still very powerful, and here is a call to the built-in HASHLIB.MD5 () method.

Verify:

After writing the Python method, we also need to verify the correctness of this Python program.

You can execute the following command on Linux:

Echo-n ' aaa ' |md5sum|cut-d '-f1

The resulting value is: 47bce5c74f589f4867dbd57e9ca9f808 is the same as the Python script.

  • Related Article

    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.