Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I generate this same token in PHP? (From .NET)

I'm having some trouble computing the same hash in PHP as I am 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 the same way:

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

However it produces a much different, larger hash:

YzgxNTdiNjVmMDMyNGY1MTcwM2I5YjA2MTE1Nzc3NjhmNjE0N2MxY2ZkODFlMzdhODIxMmJkNDRlZWJjOGYwYQ==

like image 683
Buchannon Avatar asked Oct 01 '12 18:10

Buchannon


1 Answers

Have you tried using hash_hmac with raw binary data output?

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

This seems to produce an output more like the one from .NET:

NASzFnV3Flw5ppkTIja5/aaFELPNIpfQb+kbsXCAm0Q=

in my case.

like image 103
Michal Trojanowski Avatar answered Oct 18 '22 11:10

Michal Trojanowski