Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP hash_hmac() problem with AWS

Posted on 2014-08-19
7
Medium Priority
?
310 Views
Last Modified: 2014-11-12
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/
0
Comment
Question by:Geoff Millikan
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40271795
It seems you should use "base64_encode()" function after the hashing.
0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40271800
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
 

Author Comment

by:Geoff Millikan
ID: 40275481
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
Ahmed Merghani earned 2000 total points
ID: 40277770
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
 

Author Comment

by:Geoff Millikan
ID: 40281633
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
 

Author Closing Comment

by:Geoff Millikan
ID: 40281635
Fantastic solution and well documented, easy to implement.  Thanks so much!!!
0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40281688
Glad that help and you are welcome.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question