Generate HMAC SHA256 signature in Python

By Gaurav Jain on November 10, 2017

SHA256

SHA256 encoded strings can be used to secure payment gateway.

For this problem, there is a popular function written in C# CreateSHA256Signature()
which you can find here Azadehkhojandi’s Gist.

Recently, In one of the project, I was asked to convert this function into Python. After minutes of searching on google, I had no success so decided to convert it by myself and after few minutes of hit and trial, I got this -

import hmac
import hashlib 
import binascii

def create_sha256_signature(key, message):
    byte_key = binascii.unhexlify(key)
    message = message.encode()
    return hmac.new(byte_key, message, hashlib.sha256).hexdigest().upper()

create_sha256_signature("E49756B4C8FAB4E48222A3E7F3B97CC3", "TEST STRING")

Also, hosted on Github: Gaurav Jain’s GIST