I was asked to implement HMAC in Azure Function that receives web-hooks requests but I don't find documentations that give me an idea of how to do that. Any suggestion would be great help.
I was asked to implement HMAC in Azure Function that receives web-hooks requests but I don't find documentations that give me an idea of how to do that. Any suggestion would be great help.
This is not something an azure function can do natively. You'll need to write some custom code to do the following:
Retrieve the HMAC signature sent from the webhook sender. This will likely be sent as a header on the HttpRequestData object you receive (assuming this is azure functions v4).
Compute a hash of the body of the request, using the key that is shared between you and the webhook sender. You can use the System.Security.Cryptography.HMACSHA256 class to do this.:
public string ComputeHMAC(string data, string key)
{
var dataBytes = Encoding.ASCII.GetBytes(data);
var keyBytes = Encoding.ASCII.GetBytes(key);
var hmacHash = HMACSHA256.HashData(keyBytes, dataBytes);
var hashSB = new StringBuilder();
for (int i = 0; i < hmacHash.Length; i++)
{
hashSB.Append(hmacHash[i].ToString("x2"));
}
return hashSB.ToString();
}
You could take this a step further and write an HMAC middleware if you need to do this in different azure functions.