HMAC Calculation - Examples
In Python we can calculate HMAC codes as follows (using the hashlib and hmac libraries):
1
import hashlib, hmac, binascii
2
3
def hmac_sha256(key, msg):
4
return hmac.new(key, msg, hashlib.sha256).digest()
5
6
key = b"12345"
7
msg = b"sample message"
8
print(binascii.hexlify(hmac_sha256(key, msg)))
Copied!
Run the above code example: ****https://repl.it/@nakov/HMAC-SHA256-Examples.
The above code will calculate and print the expected HMAC code (like in our previous example):
1
ee40ca7bc90df844d2f5b5667b27361a2350fad99352d8a6ce061c69e41e5d32
Copied!
Try the code yourself and play with it.
Last modified 6d ago
Copy link