Convert hash_256 in PHP to C# equivalent

Hi guys, so I tried implementing a C# equivalent of PHP’s sha256 hash generator but I am getting a different result when compared to that in PHP.

Here’s my PHP code

        $service = "AQA";
        $requestBody = '
        {
        "details": {
        "requestType": "FIND_STANDALONE_PRODUCTS"
        },
        "paymentCollectorId": "CDL",
        "paymentMethod": "PREPAID",
        "serviceId": "' . $service . '"
        }';

    $requestbodyEncode = hash('sha256', utf8_encode($requestBody));

and the result is : 13dce103015adc5804b68b5ef40f6a7eeefe4cd1341b0034868aa7a54ee6b0d6

Then here’s my C# equivalent

    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");

            var requestBody = new
            {
                details = new
                {
                    requestType = "FIND_STANDALONE_PRODUCTS"
                },
                paymentCollectorId = "CDL",
                paymentMethod = "PREPAID",
                serviceId = "AQA"
            };

            var json = Newtonsoft.Json.JsonConvert.SerializeObject(requestBody);

            byte[] bytes = Encoding.UTF8.GetBytes(json);
            var requestUtf8 = Encoding.UTF8.GetString(bytes);

            var sha256request = ComputeSha256Hash(requestUtf8);

            Console.WriteLine(json + "\n");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i].ToString("x2"));
            }
            Console.WriteLine(sb + "\n");
            
            Console.WriteLine(requestUtf8 + "\n");
            Console.WriteLine(sha256request + "\n");
        }

        static string ComputeSha256Hash(string rawData)
        {
            // Create a SHA256   
            using (SHA256 sha256Hash = SHA256Managed.Create())
            {

                //var enc = Encoding.GetEncoding(1255);
                // ComputeHash - returns byte array  
                byte[] bytes = sha256Hash.ComputeHash(Encoding.ASCII.GetBytes(rawData));

                //Convert byte array to a string
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    builder.Append(bytes[i].ToString("x2"));
                }
                return builder.ToString();
            }
        }
    }

and the result is: 53823a83b0f8f55acc7fa3d065af829f58c17da3bd62380db448713e5a2850d4

What might I be missing?

At first glance, I’d say there’s a big difference between the C# code and the PHP code as they both use different input. A single space inside the string could already result in a completely different hash value.
You should check the precise value of $requestBody in PHP, including all spaces, tabs and line breaks. Compare this with the value of json in the C# code. It would not surprise me if the PHP output looks like:

{
"details": {
"requestType": "FIND_STANDALONE_PRODUCTS"
},
"paymentCollectorId": "CDL",
"paymentMethod": "PREPAID",
"serviceId": "AQA"
}

while the C# output looks like:

{
    "details": 
    {
        "requestType": "FIND_STANDALONE_PRODUCTS"
    },
    "paymentCollectorId": "CDL",
    "paymentMethod": "PREPAID",
    "serviceId": "AQA"
}

Difference? The number of spaces…
That minor difference will result in different SHA-values.

Thank you. please how can I format the string to be as that in PHP?

To format it exactly as PHP does, you would first have to check the formatting of the PHP string. And you’d need to make an exact match.
But as you use JsonConvert, you won’t have too much control over the formatting. You could use the basic string formatting of C# with something like
var json = $"{\n\"details\": {\n\"requestType\": \"{requestType}\"\n},\n"paymentCollectorId\": \"{paymentCollectorId}\",\n\"paymentMethod\": \"{paymentMethod}\",\n\"serviceId\": \"{serviceId}\"\n}"
It’s just not very pretty. Besides, why does it need to be exactly the same hash? Normally, a hash is used to make sure the content of some value hasn’t been modified. It’s sometimes used as an index in databases too. So, if I understand what you’re trying to do with this hash, I might give some solution…

.NET Foundation Website | Blog | Projects | Code of Conduct