Why does my MD5 Hash Calculation produce wrong results

60 views Asked by At

I want to make a payment and the gateway expects an MD5# from me. l use this dictionary to calculate the hash and then submit both the hash and the dictionary to the gateway. however when l do it response with an error that the hash did not calculate correctly. This is the dictionary l used;

var returnUrl = "https://sampledomain.com/returnlink"

Dictionary<string, string> data = new Dictionary<string, string>
 {
   { "PAYGATE_ID", companyInfo.PayGateId },
   { "REFERENCE", reference },
   { "AMOUNT", $"{pGPayReqDTO.Amount}" },
   { "CURRENCY",  $"{companyInfo.PayGateCurrency}" },
   { "RETURN_URL",returnUrl },
   { "TRANSACTION_DATE", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},
   { "LOCALE", "en" },
   { "COUNTRY", $"{companyInfo.PayGateCountryCode}" },
   { "EMAIL", $"{pGPayReqDTO.Email}" },
   { "NOTIFY_URL", $"https://api.libralex.co.za/api/Paygate/Notify"}, 
};

If l use this above dictionary when l calculate the MD5# it will calculate the checksum on correctly both on my end and on the gateway. But if l change the returnUrl to

var returnUrl = $"https://{dynamicDomain}/returnLink"; 

this will fail.

im not sure why

The MD5 Calculation

   using (MD5 md5Hash = MD5.Create())
   {
       string dataString = string.Join("", data.Values) + companyInfo.PayGateSecret;

       byte[] checksumBytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(dataString));
       StringBuilder checksumBuilder = new StringBuilder();

       for (int i = 0; i < checksumBytes.Length; i++)
       {
           checksumBuilder.Append(checksumBytes[i].ToString("x2"));
       }

       data.CHECKSUM = checksumBuilder.ToString();

       // return sBuilder.ToString();
   }
0

There are 0 answers