Converting from PHP to C#
Get the solution ↓↓↓Can anyone help me converting this code from PHP to C#?
$body = "{
\"flow\": \"MATCH_CODE\",
\"amount_unit\": 100,
\"currency\": \"EUR\"
}";
$digest = base64_encode(hash("sha256", $body, true));
The first part seems to me a string declaration, the real PROBLEM is the last row.
EDIT 13/08/2021 hour 17:05
After a lot of tries, I wrote this in C#:
string requestBody = "{\"flow\": \"MATCH_CODE\",\"amount_unit\": 100,\"currency\": \"EUR\"}";
byte[] tmp = Encoding.ASCII.GetBytes(requestBody);
SHA256 sha256Hash = SHA256Managed.Create();
byte[] bytes = sha256Hash.ComputeHash(tmp);
TextBox01.Text = System.Convert.ToBase64String(bytes);
In TextBox1.Text I see "6m6lp2lwPRpHJ3BkrGF4RCAvTTgLFTqCre/kC7M5EAg="
I made a virtual machine with Xampp, and I made a sample PHP page like this:
Test Digest"; $body = "{ \"flow\": \"MATCH_CODE\", \"amount_unit\": 100, \"currency\": \"EUR\" }"; echo "body = " . $body . "
"; $aaa = hash("sha256", $body, true); echo "aaa = " . $aaa . "
"; $bbb = base64_encode($aaa); echo "bbb = " . $bbb . "
"; $digest = base64_encode(hash("sha256", $body, true)); echo "digest = " . $digest . "
"; ?>Digest is "E8oVvSVzNhmF4sskhK7XW0EnOG/likDYwI/NDVpDBKA=" Totally different from the C# result.
Answer
Solution:
These should point you in the right direction:
using SHA256.HashData
to hash a byte[] to output byte[]
System.Text.Encoding.UTF8.GetBytes
to make your body from a string to a byte[]
to make the hash output byte[] to a base64 string
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: regex stop at first match
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.