How can I generate the same token in PHP? (From .NET)

I'm having trouble calculating the same hash in PHP since I'm in C # .NET.

In C #, I have the following:

HMAC hasher = new HMACSHA256(Encoding.UTF8.GetBytes("secret")); //key
byte[] data = hasher.ComputeHash(Encoding.UTF8.GetBytes("2012-10-01T17:48:56")); //timestamp
Convert.ToBase64String(data); //computed token

Which produces something like:

yBV7ZfAyT1FwO5sGEVd3aPYUfBz9geN6ghK9RO68jwo =


In PHP, I thought this would calculate the hash in the same way:

$hmac = hash_hmac("sha256", "2012-10-01T17:48:56", "secret");
$hmac = base64_encode($hmac);

However, it produces a much larger hash:

YzgxNTdiNjVmMDMyNGY1MTcwM2I5YjA2MTE1Nzc3NjhmNjE0N2MxY2ZkODFlMzdhODIxMmJkNDRlZWJjOGYwYQ ==

+5
source share
1 answer

Have you tried using hash_hmac with the output of the original binary data?

$hmac = hash_hmac("sha256", "2012-10-01T17:48:56", "secret", true);
$hmac = base64_encode($hmac);

It seems that the result is more like the .NET result:

NASzFnV3Flw5ppkTIja5/aaFELPNIpfQb+kbsXCAm0Q=

in my case.

+3
source

All Articles