Teach you how to use python to obtain the md5 value of a string

巴扎黑
Release: 2016-12-08 11:37:11
Original
2087 people have browsed it

Recently I need to use python to get the md5 value of a string.

Post the code to share with everyone today.

  #!/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
Copy after login

Python is still very powerful. The built-in hashlib.md5() method is called here.

Verification:

After writing the python method, we still need to verify the correctness of the python program.

You can execute the following command on Linux:

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

The value obtained is: 47bce5c74f589f4867dbd57e9ca9f808 which is the same as the result of running the python script.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template