John Davidson

Is this PHP snippet equivalent to the C# snippet?

0 comments
Message:


We're given this C# snippet which generates some string to be used in a Rest API call.


We have to implement it in PHP. The client also provides us with a PHP snippet, but the string returned by PHP is different than the one returned by C#. Would you be able to tell what's wrong? I need the PHP snippet to give the same output as C#.


Here's the PHP snippet


I'll share the functions here as well, it might be easier for you to read


C#


public static string CalculateDigest(string publicKey, string privateKey, string message)
{
var digest = "";
var hash = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(privateKey));
var correctHash = string.Join(string.Empty, hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(message)).Select(b => b.ToString("x2")));
digest = publicKey + ":" + correctHash;
return digest;
}

PHP


$digest = $PUBLIC_KEY.":".Hash_hmac("sha256", $message, $PRIVATE_KEY);


My Answer:
snippet?

PHP snippet:
php
$a = 5;
$b = 10;
$c = $a + $b;
echo $c;
?>


C snippet:
c
#include

int main() {
int a = 5;
int b = 10;
int c = a + b;
printf("%d\n", c);
return 0;
}


Yes, the PHP snippet and the C snippet are equivalent in terms of functionality. Both snippets declare two integer variables `a` and `b`, assign them values, add them together, and then print the result. The only difference is the syntax and the way the result is printed (echo in PHP and printf in C).

Rate this post

4 of 5 based on 6570 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us