Cryptographic algorithms: sha1,sha224, SHA256, SHA384, SHA512, MD5
1. MD5
Import= hashlib.md5 () m.update (b"Hello") # Encryption M.update (b"It's Me") # re-encrypt a print( M.digest ()) # binary print(M.hexdigest ()) #16 binary
2.sha1
# SHA1m = hashlib.sha1 () m.update (b"Hello") # Encryption M.update (b"It's Me") # re-encrypt a print(M.digest ( ) # binary print(M.hexdigest ()) #16 binary
3. SHA512
# SHA512m = hashlib.sha512 () m.update (b"Hello") # Encryption M.update (b"It's Me") # re-encrypt a print( M.digest ()) # binary print(M.hexdigest ()) #16 binary # Other encryption methods are similar
4. If you think the above method is not enough dick, then there is an HMAC module
Import= hmac.new (b'secret_key') # generates keyH.update (b ' Hello ') # Encrypt Print (H.hexdigest ())
The following is an HMAC module use case
#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!"%Nameserver_object=MyClass () server= Simplexmlrpcserver.simplexmlrpcserver (("localhost", 8888))#(2)Server.register_instance (Server_object)#(3)Print "Listening on port 8888"Server.serve_forever ()#The code snippet is from: http://www.sharejs.com/codes/python/1880
Python (6)-Hashlib module