使用字符串计算SHA哈希+;python中的密钥

使用字符串计算SHA哈希+;python中的密钥,python,hash,sha256,Python,Hash,Sha256,Amazon产品API现在要求我尝试生成的每个请求都有一个签名 我被挂断的一步是: 使用上面的字符串和我们的“虚拟”秘密访问密钥1234567890,使用SHA256哈希算法计算符合RFC 2104的HMAC。有关此步骤的详细信息,请参阅编程语言的文档和代码示例 给定一个字符串和一个密钥(在本例中为1234567890),如何使用Python计算此哈希 -----------更新------------- 使用HMAC.new的第一个解决方案看起来是正确的,但是我得到的结果与它们不同 根据Am

Amazon产品API现在要求我尝试生成的每个请求都有一个签名

我被挂断的一步是:

使用上面的字符串和我们的“虚拟”秘密访问密钥1234567890,使用SHA256哈希算法计算符合RFC 2104的HMAC。有关此步骤的详细信息,请参阅编程语言的文档和代码示例

给定一个字符串和一个密钥(在本例中为1234567890),如何使用Python计算此哈希

-----------更新-------------

使用HMAC.new的第一个解决方案看起来是正确的,但是我得到的结果与它们不同

根据Amazon的示例,当您散列密钥1234567890和以下字符串时

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+U3AZ4OHN7TISQGS1VDLBHBEIJWCBECKL5xN9XG='

我得到了以下信息:
'411A59403C9F58B4A434C9C6A14EF6E363ACC1DB2C6FAF9ADC30E20898C83B'
来自(稍加修改):


我知道这听起来很傻,但请确保您的机密上没有意外的尾随空格。

如果您试图使用Python3向AWS cognito注册用户,您可以使用以下代码

#For the SecretHash 
import hmac
import hashlib
import base64   

//Please note that the b in the secretKey and encode('utf-8') are really really important. 
secretKey = b"secret key that you get from Coginito -> User Pool -> General Settings -> App Clients-->Click on Show more details -> App client secret  "
 clientId = "Coginito -> User Pool -> General Settings -> App Clients-->App client id"
 digest = hmac.new(secretKey,
              msg=(user_name + clientId).encode('utf-8'),
              digestmod=hashlib.sha256
             ).digest()
 secrethash = base64.b64encode(digest).decode()
上面的用户名user_name与您要在cognito中注册的用户相同

client=boto3.client('cognito-idp',region_name='eu-west-1')


如果您有stringsecret和stringtoken,它可能会有所帮助(我知道这可能为时已晚,但只是以防万一,它对某人有效)。在python 3中,这三个选项对我都有效-

import hmac
import hashlib
import base64

access_token = 'a'
app_secret = 'b'

access_token = <your token in string format>
app_secret = <your secret access key in string format>

# use any one, all three options work.
# OPTION 1 (it works)
# digest = hmac.new(app_secret.encode('UTF-8'),
#                   access_token.encode('UTF-8'), hashlib.sha256)
# OPTION 2 (it works)
# digest = hmac.new(str.encode(app_secret),
#                   str.encode(access_token), hashlib.sha256)
# OPTION 3 (it works)
digest = hmac.new(bytes(app_secret, 'UTF-8'),
                bytes(access_token, 'UTF-8'), hashlib.sha256)
signature = digest.hexdigest()
print(signature)
导入hmac
导入hashlib
导入base64
访问令牌='a'
app_secret='b'
访问令牌=
app_secret=
#使用任何一个选项,所有三个选项都有效。
#方案1(可行)
#摘要=hmac.new(应用程序加密('UTF-8'),
#access_token.encode('UTF-8'),hashlib.sha256)
#方案2(可行)
#摘要=hmac.new(str.encode(app_secret),
#str.encode(访问令牌),hashlib.sha256)
#方案3(可行)
digest=hmac.new(字节(app_secret,'UTF-8'),
字节(访问令牌'UTF-8',hashlib.sha256)
signature=digest.hexdigest()
打印(签名)

您可能需要安装py25 hashlib。我试图在Python 2.5.4(2009年3月5日)上测试这段代码,但得到了
ImportError:没有名为_md5
的模块。谢谢。这看起来是正确的,但我并没有得到与Amazon相同的结果。请参阅上面的更新。您会发现此更新非常有用。对Amazon的REST请求进行签名的算法在机密中有描述,msg需要以字节为单位,或者如果您假设是这样的话,至少要提到这一点。
>>> 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='
import hmac
import hashlib
import base64

digest = hmac.new(secret, msg=thing_to_hash, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest).decode()
#For the SecretHash 
import hmac
import hashlib
import base64   

//Please note that the b in the secretKey and encode('utf-8') are really really important. 
secretKey = b"secret key that you get from Coginito -> User Pool -> General Settings -> App Clients-->Click on Show more details -> App client secret  "
 clientId = "Coginito -> User Pool -> General Settings -> App Clients-->App client id"
 digest = hmac.new(secretKey,
              msg=(user_name + clientId).encode('utf-8'),
              digestmod=hashlib.sha256
             ).digest()
 secrethash = base64.b64encode(digest).decode()
response = client.sign_up(
                    ClientId='Coginito -> User Pool -> General Settings -> App Clients-->App client id',
                    Username='Username of the person you are planning to register',
                    Password='Password of the person you are planning to register',
                    SecretHash=secrethash,
                    UserAttributes=[
                        {
                            'Name': 'given_name',
                            'Value': given_name
                        },
                        {
                            'Name': 'family_name',
                            'Value': family_name
                        },
                        {
                            'Name': 'email',
                            'Value': user_email
                        }
                    ],
                    ValidationData=[
                        {
                            'Name': 'email',
                            'Value': user_email
                        },
                    ]
import hmac
import hashlib
import base64

access_token = 'a'
app_secret = 'b'

access_token = <your token in string format>
app_secret = <your secret access key in string format>

# use any one, all three options work.
# OPTION 1 (it works)
# digest = hmac.new(app_secret.encode('UTF-8'),
#                   access_token.encode('UTF-8'), hashlib.sha256)
# OPTION 2 (it works)
# digest = hmac.new(str.encode(app_secret),
#                   str.encode(access_token), hashlib.sha256)
# OPTION 3 (it works)
digest = hmac.new(bytes(app_secret, 'UTF-8'),
                bytes(access_token, 'UTF-8'), hashlib.sha256)
signature = digest.hexdigest()
print(signature)