我正在努力在我们的应用程序中实现 Amazon REST API。该应用程序是使用 WinDev 构建的。为了测试我的签名计算,我决定尝试亚马逊提供的测试套件: https ://docs.aws.amazon.com/general/latest/gr/signature-v4-test-suite.html
这就是我导出规范请求的十六进制值的方式:
sCanonicalRequestHash = :HashCanonicalRequest([
GET
/
Param1=value1&Param2=value2
host:example.amazonaws.com
x-amz-date:20150830T123600Z
host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
])
HashCanoncialRequest 方法删除所有字符 10(这样做是为了正确散列字符串)使用 windev 的 hashstring 函数将字符串散列为二进制。这个二进制函数被转换为十六进制值,所有的空格都被删除并变为小写。
//Remove char 13 ,otherwise the hash fails( Windows enter )
sResult = Replace(sResult, Charact(13), "")
//Create hash
sResult = HashString(HA_SHA_256, sResult)
//Convert hash to lower case hex
sResult = Lower(BufferToHexa(sResult, 1, 32))
//Remove spaces
sResult = Replace(sResult," ", "")
这会产生以下值:816cd5b414d056048ba4f7c5386d6e0533120fb1fcfa93762cf0fc39e2cf19e0
这是测试套件所期望的值。到目前为止,一切都很好。
接下来是要签名的字符串,如下所示:
AWS4-HMAC-SHA256
20150830T123600Z
20150830/us-east-1/service/aws4_request
816cd5b414d056048ba4f7c5386d6e0533120fb1fcfa93762cf0fc39e2cf19e0
现在是计算签名密钥的时候了。首先是测试套件给出的一些值:
sSecret is string = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
sDate is string = "20150830"
sRegion is string = "us-east-1"
现在计算:
bufDateKey is Buffer = WL.HashString(HA_HMAC_SHA_256, sDate, "AWS4" + sSecret)
bufRegionKey is Buffer = WL.HashString(HA_HMAC_SHA_256, sRegion, bufDateKey)
bufServiceKey is Buffer = WL.HashString(HA_HMAC_SHA_256, "service", bufRegionKey)
bufSigningKey is Buffer = WL.HashString(HA_HMAC_SHA_256, "aws4_request", bufServiceKey)
亚马逊提供了不同的测试,以便在此处检查您的计算,并且此计算经过测试并返回预期值。
现在对于不符合测试套件期望的部分。签名计算。
//Hashing the ss with psSigningKey as the key
bufSignature = WL.HashString(HA_HMAC_SHA_256, ss, bufSigningKey)
//Converting the hash to hex
bufSignature = BufferToHexa(bufSignature, 1, 32)
//Converting the hex value to lower case and remove any whitespace
bufSignature = Replace(Lower(bufSignature), " ", "")
ss 是要签名的字符串的字符串值,如第三个代码片段所示 bufSigningKey 是最后一个代码片段的结果的二进制值。这将转换为十六进制并删除所有空格并将字符串转换为小写。这不会返回测试套件所示的签名。
如果希望有人可以提供帮助。