views:

1117

answers:

5

Amazon Product API now requires a signature with every request which I'm trying to generate ushing Python.

The step I get hung up on is this one:

"Calculate an RFC 2104-compliant HMAC with the SHA256 hash algorithm using the string above with our "dummy" Secret Access Key: 1234567890. For more information about this step, see documentation and code samples for your programming language."

Given a string and a secret key (in this case 1234567890) how do I calculate this hash using Python?

----------- UPDATE -------------

The first solution using HMAC.new looks correct however I'm getting a different result than they are.

http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?rest-signature.html

According to Amazon's example when you hash the secret key 1234567890 and the following string

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

You should get the following signature: 'Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg='

I am getting this: '411a59403c9f58b4a434c9c6a14ef6e363acc1d1bb2c6faf9adc30e20898c83b'

+2  A: 

From http://docs.python.org/library/hashlib.html#module-hashlib (modified a bit):

import hashlib
secretKey = "1234567890"
m = hashlib.sha256()

# Get string and put into givenString.

m.update(givenString + secretKey)
m.digest()
Andrew Keeton
Argh! I was 8 seconds too late! ;)
ire_and_curses
You may need to install py25-hashlib. I tried to test this code on Python 2.5.4 (March 5, 2009) but got `ImportError: No module named _md5`.
Andrew Keeton
+6  A: 
import hmac
import hashlib
import base64
dig = hmac.new(b'1234567890', msg=your_bytes_string, digestmod=hashlib.sha256).digest()
base64.b64encode(dig).decode()      # py3k-mode
'Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg='
SilentGhost
Thanks. This looks correct but I am not yielding the same results as Amazon. See the update above.
Their hash looks like it's base64 encoded.
Eli
That's exactly it. Needed to encode in base64. Thanks.
+2  A: 
>>> import hmac
>>> import hashlib
>>> import base64
>>> s = """GET
... webservices.amazon.com
... /onca/xml
... AWSAccessKeyId=00000000000000000000&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReviews&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-06"""
>>> base64.b64encode(hmac.new("1234567890", msg=s, digestmod=hashlib.sha256).digest())
'Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg='
Filip Salomonsson
A: 

You can find this one useful. The algorithm of signing a REST request to Amazon is described in http://stackoverflow.com/questions/1088715/how-to-sign-amazon-web-service-requests-from-the-python-app-engine/1343917#1343917

alsan
A: 

Hi,

I tried some of the versions proposed above:

with the code of SilentGhost I get

A8m9YAke1q/UiXXqTB0VY+D+CoS9+y5U7w+/lmBWwiA=

with the code of Filip Salomonsson I get

wA3v8g9YERdWgNGZA1D+Yra80/u0S5LqPwis2ZI2ml0=

I am surprised that both versions, using different strings yield the same signature...

What am I doing wrong?

please ask your own question and link to this one. then delete this non-answer. thanks
SilentGhost