Home > Article > Backend Development > Teach you how to use python to obtain the md5 value of a string
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
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.