Hace
Hace

Reputation: 1421

How to get Ogone to support special characters in field values with SHA-512

We implemented SHA-512 hashing for the Ogone payment platform. This means we have to hash all values of all the FORM variables.

However, whenever a field has special characters the Hash is not accepted by Ogone. The payment page will than be rendered with the documented error: error order/1/s

Ogone has a test-page available to test a string and get SHA512: https://secure.ogone.com/ncol/test/testsha_UTF8.asp

Here are the examples that I used for unit testing.

[TestMethod]
public void EncodePaiosBajos_ResultsInExpectedSha512String()
{
  string name = "Paios Bajos";
  string result = PaymentProviderHelper.ShaEncode(name, new SHA512Managed());
  string expected = "3AC80D8A5B1AF7DF3530027269B807B097B6C770F168509ED7A8148E941AEA3369112F1B5BF1BE0965D0FE9AB83C1AB1CB7BF22DFD733E120A6A828600EFE643";
  Assert.AreEqual(expected, result);
}

[TestMethod]
public void EncodePaiosBajosSpecialCharacters_ResultsInExpectedSha512String()
{
  string name = "Países Bajos";
  string result = PaymentProviderHelper.ShaEncode(name, new SHA512Managed());
  string expected = "D4F8FFEC3FA0A3146C9611294569FF861CFF0E8C26982DA8D492DAB5D67B380558BFA0571A7D0DF49407B5075AE4DCE76253603075AF9350EA180ED32FA96026";
  Assert.AreEqual(expected, result);
}

The PaymentProviderHelper.ShaEncode method looks like this:

 internal static string ShaEncode(string signature, HashAlgorithm shaEncoding)
        {
            shaEncoding.Initialize();
            // Encode signature
            byte[] buffer = Encoding.ASCII.GetBytes(signature);
            shaEncoding.ComputeHash(buffer);

            // Return the encoded string in hexidecimal
            String returnValue = String.Empty;
            foreach (byte c in shaEncoding.Hash)
            {
                returnValue += String.Format("{0:X2}", (int)c);
            }

            return returnValue;
        }

Question: Why does the second unit test fail?

Is there something wrong in this Encode implementation for special characters perhaps?

Tests are in C#.NET 4.5

Upvotes: 1

Views: 651

Answers (1)

k.m
k.m

Reputation: 31444

The ogone's test page uses UTF8 encoding (basing on URL) to product hashes. Yet in your implementation you use Encoding.ASCII. Simply change your implementation encoding to Encoding.UTF8.

Upvotes: 1

Related Questions