python3 hmac module

hmac: hex-based message authentication code Hash Message Authentication Code

Note that the incoming message is key and bytestype, strthe type of coding is required first bytes.

 

import hmac

secret_key1 = b'This is my secret key'
message1 = b'Hello world'
hex_res1 = hmac.new(secret_key1, message1, digestmod="MD5").hexdigest()
print(hex_res1)  # b8908a20bd70f465330b434e18441d3b

secret_key2 = b'This is my secret key'
message2 = b'Hello world'
hex_res2 = hmac.new(secret_key2, message2, digestmod="MD5").hexdigest()
print(hex_res2)   # b8908a20bd70f465330b434e18441d3b 

compare_res = hmac.compare_digest (hex_res1, hex_res2)   # compare two are the same ciphertext 
Print (compare_res)   # True 

secret_key3 = B ' This IS My Secret Key ' 
Message3 = B ' the Hello World! ' 
hex_res3 = HMAC .new (secret_key3, Message3, digestmod = " the MD5 " ) .hexdigest ()
 Print (hex_res3)   # a314490e13ff3d1dfa9cd18db8c4c3e8 

compare_res = hmac.compare_digest (hex_res1, hex_res3)   # compare two are the same ciphertext
print(compare_res)  # False

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/10942922.html