I am trying to validate my Function App Secret Key, that is passed from Github Webhook, using .NET CORE 3.1.
In my Github webhook, I inserted default key from Azure function into "Secret" field. Now, I am trying to validate it in my code. For some reason my encrypted secret key is different from the one in webhook.
NOTE: Secret from Github Webhook is encrypted with SHA1 algorithm.
Code:
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
var secretKey = "my_key";
StringValues outHeader;
if (req.Headers.TryGetValue("x-hub-signature", out outHeader))
{
log.LogWarning("==========");
log.LogWarning(outHeader);
log.LogWarning(GetHash(secretKey));
log.LogWarning("==========");
}
string responseMessage = "Everything went well!";
return new OkObjectResult(responseMessage);
}
public static string GetHash(string input)
{
return "sha1=" + string.Join("",
(new SHA1Managed()
.ComputeHash(Encoding.UTF8.GetBytes(input)))
.Select(x => x.ToString("x2"))
.ToArray());
}
Output:
2020-12-13T16:46:47.592 [Warning] ==========
2020-12-13T16:46:47.592 [Warning] sha1=f859bebbf5ec452a7ecd42efc69e0d86a4f25b16
2020-12-13T16:46:47.593 [Warning] sha1=fa1167715f137edff21d55d00adf63afb318b2a6
2020-12-13T16:46:47.593 [Warning] ==========
Official docs covers Node.js solution only.
What is the right way to validate Github Webhook Secret in .NET CORE 3.1? Thank you for any help.