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