HMAC Calculation - Examples in Python

In Python we can calculate HMAC codes as follows (using the hashlib and hmac libraries):

import hashlib, hmac, binascii

def hmac_sha256(key, msg):
  return, msg, hashlib.sha256).digest()

key = b"12345"
msg = b"sample message"
print(binascii.hexlify(hmac_sha256(key, msg)))

Run the above code example:

The above code will calculate and print the expected HMAC code (like in our previous example):


Try the code yourself and play with it.

results matching ""

    No results matching ""