Introduction to the encryption of the 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 HASHLIBM = Hashlib.md5 () m.update (b "Hello") print (M.hexdigest ()) m.update (b "It ' s Me") print (M.hexdigest ()) M.update (b "It ' s been a long time since we spoken.") Print (M.hexdigest ()) m2 = Hashlib.md5 () m2.update (b "Helloit ' s Me") print (M2.hexdigest ()) # Output 5d41402abc4b2a76b9719d911017c59264f69d95135bc13d4827f871b37f780f0c9a83e10aa2f9e9629be61146db9cc264f69d95135bc13d4827f87 1b37f780f# the second and fourth MD5 values are the same, so the second one is encrypted Helloit ' SME

  print(m.digest()) #2进制格式hash

  print ( len (m.hexdigest())) #16进制格式hash
# ######## SHA1 ######## hash = HASHLIB.SHA1 () hash.update (' admin ') print (Hash.hexdigest ()) # ######## sha256 ######## Hash = hashlib.sha256 () hash.update (' admin ') print (Hash.hexdigest ())  # ######## sha384 ######## hash = hashlib.sha384 () Hash.update (' admin ') print (Hash.hexdigest ()) # ######## sha512 ######## hash = hashlib.sha512 () hash.update (' admin ') Print (Hash.hexdigest ())

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

Hash message authentication code, or HMAC, is an authentication mechanism based on the message identification Code (authentication code) of Mac. When using HMAC, both sides of the message communication authenticate the authenticity of the message by verifying the authentication key K added in the message;

Generally used in network communication message encryption, the premise is that both parties must first agree on a good key, like a connector password, and then send the message with key to encrypt the message, the receiver with key + message plaintext again encrypted, take the encrypted value with the sender's relative ratio is equal, so that the authenticity of the message can be verified, And the legitimacy of the sender.

import Hmach = Hmac.new ("King of the Land Tiger". Encode (encoding= "Utf-8"), "You are a little squirrel". Encode ( encoding= "Utf-8") print (H.digest ()) print (H.hexdigest ()) #输出b ' fx\xad\xdd\x9e\xd6\xddcqn\x82c\xcd\xd9\ x80-' 6678addd9ed6dd63514e8263cdd9802d 

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.