PHP hash_hmac() problem with AWS

I'm following the dev guide at the link below which says if I HMAC hash the below string with the key "1234567890" then I should get a value of "M/y0+EAFFGaUAp4bWv/WEuXYah99pVsxvqtAuC8YN7I=" but I get "2cce52e8caf2714113bb5a7de100e72eeaca229c22a8a9c2c66dc1ef76b44221"

What am I doing wrong?
My PHP code (run it at Linux command line):
php -r "echo hash_hmac('sha256',file_get_contents('/home/path/to/file/amazon.txt'),1234567890,FALSE);"

Open in new window

String to hash:
GET
webservices.amazon.com
/onca/xml
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&ItemId=0679722769&Operation=I
temLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReview
s&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&
Version=2009-01-06

Open in new window

http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

Thanks!

http://www.t1shopper.com/
Geoff MillikanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ahmed MerghaniSoftware EngineerCommented:
It seems you should use "base64_encode()" function after the hashing.
0
Ahmed MerghaniSoftware EngineerCommented:
Try this:

php -r "echo base64_encode(hash_hmac('sha256',file_get_contents('/home/path/to/file/amazon.txt'),1234567890,TRUE));"

Open in new window

0
Geoff MillikanAuthor Commented:
Good idea, but the resulting hash I'm getting with that is:

upLgv6NdigGIMfNnXwtDFMhGjU2W5pgbhbozkMIzCP0=

I'm wondering if my carriage returns are incorrect?  Like maybe they should be \r but I have them as \r\n because that would change the hash output.  Dang hashes.
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

Ahmed MerghaniSoftware EngineerCommented:
Yes that is right. So we have 2 issues:
1- The line breaks used is line feed "LF" or "\n" which means we will strip the carriage returns "CR" or "\r".
So, your php command line will be:
php -r "echo base64_encode(hash_hmac('sha256',str_replace("\r", "", file_get_contents('/home/path/to/file/amazon.txt')),1234567890,TRUE));"

Open in new window

2- The parameter/value pairs list "MUST" be in one line not multi lines.
So, your file will be as the attached "amazon.txt" file.

If still there some problems, I do the same solution in php script file attached as "hash.php".
amazon.txt
hash.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geoff MillikanAuthor Commented:
Arg!  Can't believe I neglected to remove the line wrapping in the URL!  How silly of me...

Fantastic solution!!  Thanks so much!!

GET
webservices.amazon.com
/onca/xml
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReviews&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-06

Open in new window


php -r "echo base64_encode(hash_hmac('sha256',str_replace('\r', '', file_get_contents('/home/path/to/file/amazon.txt')),1234567890,TRUE));"

Open in new window

0
Geoff MillikanAuthor Commented:
Fantastic solution and well documented, easy to implement.  Thanks so much!!!
0
Ahmed MerghaniSoftware EngineerCommented:
Glad that help and you are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.