I have set up an endpoint to receive webhook requests from Shopify.
The requests from Shopify include an HMAC header that is created from a shared secret key and the body of the request.
I need to calculate the HMAC on my server and match it to the value in the request header to ensure that the request is authentic.
I can't seem to create the appropriate mechanism in .NET to create a matching HMAC value.
My algorithm at this point is as follows:
public static string CreateHash(string data)
{
string sharedSecretKey = "MY_KEY";
byte[] keyBytes = Encoding.UTF8.GetBytes(sharedSecretKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
//use the SHA256Managed Class to compute the hash
System.Security.Cryptography.HMACSHA256 hmac = new HMACSHA256(keyBytes);
byte[] hmacBytes = hmac.ComputeHash(dataBytes);
//retun as base64 string. Compared with the signature passed in the header of the post request from Shopify. If they match, the call is verified.
return System.Convert.ToBase64String(hmacBytes);
}
The Shopify docs for verifying their webhooks can be found HERE but only PHP and Ruby samples are included.
Can anyone see what I might be doing wrong? Should I be just passing the entire JSON request body as a string into this method?