如何在python中计算AWS API签名(v4)? [英] How can I calculate an AWS API signature (v4) in python?
问题描述
我正在尝试使用示例函数,但我无法使其正常工作.在这一点上,我确定我缺少了一个非常明显的东西:
I'm attempting to generate a signature for an Amazon Glacier upload request, using the example requests and example functions provided by the AWS documentation, but I can't make it work. At this point, I'm certain I'm missing something incredibly obvious:
#!/bin/env python
import hmac
import hashlib
# This string to sign taken from: http://docs.amazonwebservices.com/amazonglacier/latest/dev/amazon-glacier-signing-requests.html#example-signature-calculation
sts = """AWS4-HMAC-SHA256
20120525T002453Z
20120525/us-east-1/glacier/aws4_request
5f1da1a2d0feb614dd03d71e87928b8e449ac87614479332aced3a701f916743"""
# These two functions taken from: http://docs.amazonwebservices.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).hexdigest()
# The fake secret key is provided by the referenced docs
def getSignatureKey():
kDate = sign(("AWS4" + "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY").encode('utf-8'), "20120525")
kRegion = sign(kDate, "us-east-1")
kService = sign(kRegion, "glacier")
kSigning = sign(kService, "aws4_request")
return kSigning
signature = sign(getSignatureKey(), sts)
print signature
如果我运行程序,则会得到以下哈希值:
If I run my program, I get the following hash:
$ python test.py
3431315da57da4df28f92895c75364d94b36c745896ad3e580c0a6ae403b1e05
但是文档中明确指出:
如果使用秘密访问密钥wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY,则计算出的签名为:
If the secret access key, wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY, is used, then the calculated signature is:
3ce5b2f2fffac9262b4da9256f8d086b4aaf42eba5f111c21681a65a127b7c2a
3ce5b2f2fffac9262b4da9256f8d086b4aaf42eba5f111c21681a65a127b7c2a
我想念什么?
推荐答案
您的功能在一个方面与他们的功能有所不同.你在做
Your function differs from theirs in one respect. You're doing
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).hexdigest()
但是他们在做
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
因此您的派生密钥是错误的.您只想在过程的最后一步使用hexdigest,而不是在计算签名密钥时使用.
So your derived key is wrong. You only want to be using hexdigest at the last step of the process, not when calculating the signing key.
这篇关于如何在python中计算AWS API签名(v4)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!