Hashlib module -- encryption, hashlib module --
Used for encryption-related operations. In 3.x, the md5 module and sha module are replaced. The SHA1, sha1_, SHA256, SHA384, SHA512, and MD5 algorithms are mainly provided.
Import hashlibm = hashlib. md5 () m. update (B "hello") print (m. hexdigest () m. update (B "It's me") print (m. hexdigest () m. update (B "It's been a long time since we spoken. ") print (m. hexdigest () m2 = hashlib. md5 () m2.update (B "helloIt's me") print (m2.hexdigest () # output Digest # the second and fourth md5 values are the same, so the second encrypted version is helloIt's sme.
print
(m.digest())
# Binary hash
print
(
len
(m.hexdigest()))
# Hexadecimal hash
# ######## sha1 ######## hash = hashlib.sha1()hash.update('admin')print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256()hash.update('admin')print(hash.hexdigest()) # ######## sha384 ######## hash = hashlib.sha384()hash.update('admin')print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512()hash.update('admin')print(hash.hexdigest())
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 hmach = hmac. new ("Tianwang gaidihu ". encode (encoding = "UTF-8"), "You are a squirrel ". encode (encoding = "UTF-8") print (h. digest () print (h. hexdigest () # output B 'fx \ xad \ xdd \ x9e \ xd6 \ xddcQN \ x82c \ xcd \ xd9 \ x80-'6678addd9ed6dd63514e8263cdd9802d