在python中使用字符串+密钥计算SHA哈希 [英] Calculating a SHA hash with a string + secret key in python
问题描述
Amazon Product API 现在需要对我尝试使用 Python 生成的每个请求进行签名.
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:
使用上面的字符串和我们的虚拟"秘密访问密钥:1234567890,使用 SHA256 哈希算法计算符合 RFC 2104 的 HMAC.有关此步骤的更多信息,请参阅您的编程语言的文档和代码示例."
"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."
给定一个字符串和一个密钥(在本例中为 1234567890)我如何使用 Python 计算这个哈希?
Given a string and a secret key (in this case 1234567890) how do I calculate this hash using Python?
----------- 更新 -------------
----------- UPDATE -------------
第一个使用 HMAC.new 的解决方案看起来是正确的,但我得到的结果与它们不同.
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
根据亚马逊的示例,当您对密钥 1234567890 和以下字符串进行散列时
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
您应该获得以下签名:'Nace+U3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg='
我得到这个:'411a59403c9f58b4a434c9c6a14ef6e363acc1d1bb2c6faf9adc30e20898c83b'
推荐答案
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='
这篇关于在python中使用字符串+密钥计算SHA哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!