Based on hashlib module Encryption of detail

  • 2020-06-07 04:41:00
  • OfStack

3.x replaces md5 module and sha module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithms


import hashlib
 
m = 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())
 
#  The output 
5d41402abc4b2a76b9719d911017c592
64f69d95135bc13d4827f871b37f780f
0c9a83e10aa2f9e9629be61146db9cc2
64f69d95135bc13d4827f871b37f780f
 
# The first 2 And the first 4 a  md5 The values are the same, so number one 2 It's encrypted  helloIt'sme<br><code class="python comments"><br></code>

print(ES14en. digest()) #2 base format hash

print(len(ES21en. hexdigest())) # hexadecimal format 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 the key we created and the content we encrypted

Hash message authentication code, HMAC for short, is an authentication mechanism based on message authentication code MAC (Message Authentication Code). When using HMAC, both parties of the message communication can verify the authenticity of the message by verifying the authentication key K added to the message.

1 used in network communication message encryption, premise is that the two sides agreed to good key first, like joint sign sample 1, then the message is sent to the message encryption with key, receiver with key + message plaintext encrypted again, take the encrypted value compared to the sender's relative are equal, so you can verify the authenticity of the news, and the legitimacy of the sender.


import hmac
 
h = hmac.new(" The king of heaven covers the earth tiger ".encode(encoding="utf-8"), " You're a little squirrel ".encode(encoding="utf-8"))
print(h.digest())
print(h.hexdigest())
 
# The output 
b'fx\xad\xdd\x9e\xd6\xddcQN\x82c\xcd\xd9\x80-'
6678addd9ed6dd63514e8263cdd9802d

Related articles: