using System.Collections.Generic;
using System.Security.Cryptography;
static string GetHmacHash(string input, string secret)
if (string.IsNullOrWhiteSpace(input)) return "";
Encoding encoding = Encoding.UTF8;
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(input);
using (HMACSHA256 hmacsha256 = new HMACSHA256(keyByte))
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return BitConverter.ToString(hashmessage).Replace("-", "").ToLower();
public static void Main()
var signatureKey = "DTest";
var signatureHeader = "t=1669794735,hmac_sha256=8b915c6baf2e6c3fe1deeebdd9d39d736c4f1345f23dee07129770cc00d93cfb";
var signature = new Dictionary<string, string>();
foreach (var pair in signatureHeader.Split(',')) {
var requestString = "{'data':{'client':{'paymentInitiations':{'eventId':'90400463-ab37-4cab-8ced-da724015e0a5','subscriptionId':'YmFhNTNhYmUtZTViOC00M2UyLWEzMGQtZTZlNTE4OGZjZGJl','time':'2022-11-30T07:52:15.498Z','node':{'id':'cGF5aW5pdC9hOGZhMzc5Yy1iMmFhLTQ2NjYtOTEzZS1hZGY2MmRkYzU4ODc=','externalReference':'SM20221130834269794718967','beneficiaryReference':'10008685','date':'2022-11-30T07:52:15.074Z','status':{'__typename':'PaymentInitiationCompleted','date':'2022-11-30T07:52:15.238Z','payer':{'__typename':'PaymentInitiationBankAccountPayer','accountName':'FNB Premier Cheque Account','accountType':'current','accountNumber':'62120098985','bankId':'fnb'}}}}}}}";
var request = JsonConvert.DeserializeObject(requestString);
string body = JsonConvert.SerializeObject(request);
string hashComputeInput = signature["t"] + "." + body;
string computedHash = GetHmacHash(hashComputeInput, signatureKey);
var incomingHash = signature.GetValueOrDefault("hmac_sha256");
if (computedHash == incomingHash)
Console.WriteLine("Computed hash and incoming hash match");
throw new WebException($"Could not match computed {computedHash} with incoming {incomingHash}");