Python hashlib module

Source: Internet
Author: User

For cryptographic related operations, the 3.x replaces the MD5 module and the SHA module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

Import Hashlib m=hashlib.md5 () # m=hashlib.sha256 () m.update (' Hello '. Encode (' UTF8 ')) print (M.hexdigest ())  # 5d41402abc4b2a76b9719d911017c592 m.update (' Alvin '. Encode (' UTF8 ')) print (M.hexdigest ())  # 92a7e713c30abbb0319fa07da2a5c4af m2=hashlib.md5 () m2.update (' Helloalvin '. Encode (' UTF8 ')) print (M2.hexdigest ()) # 92a7e713c30abbb0319fa07da2a5c4af

Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do the encryption

Import Hashlib # ######## ######## hash = hashlib.sha256 (' 898oafs09f '. Encode (' UTF8 ')) hash.update (' Alvin '. Encode (' UTF8 ') print (Hash.hexdigest ()) #e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7

Python also has an HMAC module that internally creates key and content for us to process and then encrypt:

Import Hmach = Hmac.new (' Alvin '. Encode (' UTF8 ')) h.update (' Hello '. Encode (' UTF8 ')) print (H.hexdigest ()) # 320df9832eab4c038b6c1d7ed73a5940

Python hashlib module

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.