Python Basic Learning Log day5--hashlib module

Source: Internet
Author: User
Tags hmac sha1 sha256 algorithm

The Hashlib module is used for cryptographic operations, instead of the MD5 and SHA modules,

Mainly provide SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm.

#-*-coding:utf-8-*-__author__='Shisanjun'ImportHASHLIBM=hashlib.md5 () #使用MD5算法m. Update (b"Hello") #必须加b, stated as Bytem.update (b"It is me")Print(M.digest ())#2 binary format hashM.update (b"Hello It is me")Print(M.digest ())Print(M.hexdigest ())#16 binary format hash"""def digest (self, *args, **kwargs): # Real Signature Unknown"""Return the digest value as a string of binary data."""passdef hexdigest (self, *args, **kwargs): # Real Signature Unknown"""Return the digest value as a string of hexadecimal digits."""Pass"""

#-*-coding:utf-8-*-__author__='Shisanjun'ImportHashlib#MD5 algorithm EncryptionHash=hashlib.md5 () hash.update (b'Admin')Print(Hash.hexdigest ())#SHA1 algorithm EncryptionHash=hashlib.sha1 () hash.update (b"Admin")Print(Hash.hexdigest ())#sha256 algorithm EncryptionHash=hashlib.sha256 () hash.update (b"Admin")Print(Hash.hexdigest ())#sha512 algorithm EncryptionHash=hashlib.sha512 () hash.update (b"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.

>>>ImportHMAC>>>PrintHmac.new ("MyKey","Hello World!"). Hexdigest () D157e0d7f137c9ffc8d65473e038ee86#The following is a simple c/s program, using the HMAC signature #Client (signs the data)ImportXmlrpclib,hmac,hashlibkey="MySecret"Server= Xmlrpclib. Serverproxy ("http://localhost:8888") name="Homer"Signature=hmac.new (key,name). Hexdigest ()PrintServer.sayhello (signature,name)#Server (verifies the signature)ImportSimplexmlrpcserver,hmac,hashlibkey="MySecret" classMyClass:defSayHello (self, Signature, name):ifHmac.new (Key,name). Hexdigest ()! =Signature:return "wrong signature! You ' re a hacker!"        Else:            returnU"Hello,%s!"%name Server_object=MyClass () server= Simplexmlrpcserver.simplexmlrpcserver (("localhost", 8888))#(2)Server.register_instance (Server_object)#(3)Print "Listening on port 8888"Server.serve_forever ()

Python Basic Learning Log day5--hashlib module

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.