How to compute SHA256-HMAC hash in Python?

How can I use SHA256-HMAC in Python?

I am trying to compute a SHA256-HMAC hash using a message and key taken from the following URL:

import hmac
import hashlib
import base64
my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"
print(hashlib.sha256(my + key).hexdigest())

However, when I run this code, I get the following result:

2df1d58a56198b2a9267a9955c31291cd454bdb3089a7c42f5d439bbacfb3b88

But I am expecting the following result:

adcb671e8e24572464c31e8f9ffc5f638ab302a0b673f72554d3cff96a692740

How can I use Python hmac to properly compute the SHA256-HMAC hash?

Sure, let me share a straightforward way to do it. Using the hmac module combined with hashlib is the most common approach. Here’s how it works:

import hmac
import hashlib

my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"

# Create HMAC object and compute the hash
hmac_obj = hmac.new(key.encode(), my.encode(), hashlib.sha256)
print(hmac_obj.hexdigest())

Explanation: The hmac.new() method is used to create an HMAC object. Both the key and message are converted to bytes, and hashlib.sha256 ensures the hash is computed using SHA256. Simple and effective for most use cases.

That’s a great start, Akansha! Let me add to that. In case you need the output in base64 format, which is often the case when working with APIs or specific encodings, you can tweak it slightly. Here’s the updated approach:

import hmac
import hashlib
import base64

my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"

# Compute HMAC SHA256
hmac_obj = hmac.new(key.encode(), my.encode(), hashlib.sha256)
hmac_digest = hmac_obj.digest()

# Convert the binary digest to base64
hmac_base64 = base64.b64encode(hmac_digest).decode()
print(hmac_base64)

Explanation: This builds on the previous method but adds base64.b64encode() to convert the binary hash to a base64 string. Handy when working in scenarios that require base64 encoding!

Using hashlib.new() with hmac (advanced option) If you want to work with a more flexible hashing approach, you can use hashlib.new() along with hmac:

import hmac
import hashlib

my = "/api/embedded_dashboard?data=%7B%22dashboard%22%3A7863%2C%22embed%22%3A%22v2%22%2C%22filters%22%3A%5B%7B%22name%22%3A%22Filter1%22%2C%22value%22%3A%22value1%22%7D%2C%7B%22name%22%3A%22Filter2%22%2C%22value%22%3A%221234%22%7D%5D%7D"
key = "e179017a-62b0-4996-8a38-e91aa9f1"

Using hashlib.new to create an HMAC object

hmac_obj = hmac.new(key.encode(), my.encode(), hashlib.new('sha256'))
print(hmac_obj.hexdigest())

Explanation: This advanced solution uses hashlib.new() to dynamically choose the SHA256 algorithm, providing more flexibility for working with different algorithms in the future.