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==
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With