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