Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP hash_hmac() OS difference
    text
    copied!<p>I need to sign my Amazon Product API request, the accepted code to do this is the following.</p> <pre><code>base64_encode(hash_hmac('sha256', $request, $key, true)); </code></pre> <p>This works great when I'm on 64bit Linux but fail when I'm on 64bit Windows 7. Has anyone got any ideas how to debug why hash_hmac provides different output between 64bit Linux and 64bit Windows?</p> <p>Test code:</p> <pre><code>&lt;?php $test = "GET webservices.amazon.com /onca/xml AWSAccessKeyId=00000000000000000000&amp;ItemId=0679722769&amp;Operation=ItemLookup&amp;ResponseGroup=ItemAttributes%2COffers%2CImages%2CReviews&amp;Service=AWSECommerceService&amp;Timestamp=2009-01-01T12%3A00%3A00Z&amp;Version=2009-01-06"; #echo $test; echo base64_encode(hash_hmac('sha256', $test, '1234567890', true)); </code></pre> <p><strong>Edit</strong> The correct output of the code above is <code>Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg=</code> The incorrect output is <code>X0UTct9XSJ/3k2gu6noyKhTlksx5ZbH4qAbCyW3zX48=</code></p> <p>The non-base64-encoded values are <code>5f451372df57489ff793682eea7a322a14e592cc7965b1f8a806c2c96df35f8f</code> for the incorrect and <code>35a71ef94dc0cf83a137bb484aa82cd6f74b0470448a359c05e0aa2f9c4df718</code> for the correct one when converted to hex. (it's raw binary output in the call)</p> <p><strong>Edit 2</strong> My guess right now is that even though the Windows 7 OS is 64bit the PHP binary is compiled as 32bit as I could not find a 64bit version on the php.net site.</p> <p><strong>Edit 3</strong> I'm actually pretty sure now that the problem is that the binary is 32bit. Most likely PHP on linux uses an OS version to calculate the hash while PHP on windows implements it's own version which causes the 32/64bit difference even though both OS are 64bit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload