Calcular un hash SHA con una cadena + clave secreta en python


Amazon Product API ahora requiere una firma con cada solicitud que estoy tratando de generar usando Python.

El paso en el que me cuelgo es este:

"Calcule un HMAC compatible con RFC 2104 con el algoritmo hash SHA256 utilizando la cadena anterior con nuestra Clave de acceso secreta "ficticia": 1234567890. Para obtener más información sobre este paso, consulte documentación y ejemplos de código para su lenguaje de programación."

Dada una cadena y una clave secreta (en este caso 1234567890) Calculo este hash usando Python?

----------- ACTUALIZACIÓN -------------

La primera solución usando HMAC.nuevo parece correcto sin embargo estoy obteniendo un resultado diferente de lo que son.

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

De acuerdo con el ejemplo de Amazon al hacer hash en la clave secreta 1234567890 y la siguiente cadena

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

Debe obtener la siguiente firma: 'Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg='

Yo soy obteniendo esto: '411a59403c9f58b4a434c9c6a14ef6e363acc1d1bb2c6faf9adc30e20898c83b'

Author: SilentGhost, 2009-08-20

4 answers

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='
 83
Author: SilentGhost,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-20 17:12:24
>>> 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='
 11
Author: Filip Salomonsson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-20 16:41:26
import hmac
import hashlib
import base64

digest = hmac.new(secret, msg=thing_to_hash, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest).decode()

Sé que esto suena tonto, pero asegúrate de no tener un espacio al final de tu secreto por accidente.

 9
Author: George Campbell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-28 19:13:36

De http://docs.python.org/library/hashlib.html#module-hashlib (modificado un poco):

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

# Get string and put into givenString.

m.update(givenString + secretKey)
m.digest()
 2
Author: Andrew Keeton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-08-20 14:43:52