Day6 hashlib module and day6hashlib Module
Hashlib Module
Used for file operations related to encryption. 3. X replaces the md5 module and sha module. It mainly provides SHA1, shaloud, SHA256, SHA384, SHA512, and MD5 algorithms.
_ Always_supported = ('md5', 'sha1', 'shawei', 'sha256 ', 'sha384', 'sha512 ')
Import hashlibm = hashlib. md5 () m. update (B "Hello") m. update (B "It's me") print (m. digest () m. update (B "It's been a long time since last time we... ") print (m. digest () # binary format hashprint (m. hexdigest () # hexadecimal 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 ''' hash = hashlib. sha512 () hash. update (B 'admin') print (hash. hexdigest ())
The above code process is the encryption process. We can use many methods for encryption. First, make sure you use that method for encryption and use update () for encryption.
Not enough? Python also has an hmac module, which internally processes and encrypts the key and content.
HMAC is a Message Authentication mechanism based on the Message Authentication Code MAC. When HMAC is used, both parties of the message communication can identify the authenticity of the message by verifying the authentication key K added to the message;
It is generally used for message encryption in network communication, provided that both parties have to agree on the key first, just like the connector password, and then the message is sent to encrypt the message with the key, the receiver uses key + message plaintext for re-encryption, and checks whether the encrypted value is equal to the relative ratio of the sender. In this way, the authenticity of the message and the legality of the sender can be verified.
>>> Import hmac
>>> H = hmac. new (B "pipei", B "alex ")
>>> H. hexdigest ()
'3c7ffaf55fc7a53f678debb2adca855f'
>>> H = hmac. new (B "pipei ")
>>> H. update (B "alex ")
>>> H. hexdigest ()
'3c7ffaf55fc7a53f678debb2adca855f'