I need to pass a SHA256 hash to some 3rd party API. They have even provided a sample in JS which works fine and the results is matched with online SHA256 generator like this one:
But the problem occurs when I use C# to generate the SHA256 hash.
I've matched almost everything like input, encoding; etc. But I'm not exactly sure what the issue actually.
Please check the input which behave differently below
Input
{
"correlation_identifier": "Payee Validation",
"context": "PAYM",
"creditor_account": "23124812341",
"creditor_name": "NARUMATT",
"uetr": "3c9e595f-07d8-4f56-ae80-8edbb7683d59",
"creditor_agent": {
"bicfi": "BSALSVSS"
}
}
Online Results
a425b580c44e2c3ba6ce213529818feea5bd7ccfd76ccd02171c87ff1e53c8ee
C# Results
b192e9203176649de967fa4cbe9e513a77fc29797981ea1ce93e6e07e5dd94e3
c# Code
string inputString = JsonConvert.SerializeObject(new Input()
{
correlation_identifier = "Payee Validation",
context = "PAYM",
creditor_account = "23124812341",
creditor_name = "NARUMATT",
uetr = "3c9e595f-07d8-4f56-ae80-8edbb7683d59",
creditor_agent = new CreditorAgent()
{
bicfi = "BSALSVSS"
}
}, Formatting.Indented);
string base64Hash = GenerateSha256(inputString);
static string GenerateSha256(string inputString)
{
var hash = new System.Text.StringBuilder();
using (SHA256 sha256 = SHA256.Create())
{
byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(inputString));
foreach (byte theByte in hashBytes)
{
hash.Append(theByte.ToString("x2"));
}
}
return hash.ToString();
}
JS Sample
var CryptoJS = require('crypto-js')
var reqBodyUtf8 = CryptoJS.enc.Utf8.parse(pm.request.body.raw);
var reqBodyDigest = CryptoJS.SHA256(reqBodyUtf8).toString(CryptoJS.enc.Base64);