亞馬遜有一些examples用HMAC簽名他們的REST呼叫。但是,在Metro/WinRT應用程序中使用以下代碼時,簽名不匹配。亞馬遜REST呼叫的HMAC簽名在WinRT上不正確
方法計算HMAC:
internal string CreateHMAC(
string message,
string algorithmName,
string key)
{
MacAlgorithmProvider macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm(algorithmName);
var binaryMessage = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);
var binaryKeyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
var hmacKey = macAlgorithmProvider.CreateKey(binaryKeyMaterial);
var binarySignedMessage = CryptographicEngine.Sign(hmacKey, binaryMessage);
var signedMessage = CryptographicBuffer.EncodeToBase64String(binarySignedMessage);
return signedMessage;
}
測試,以檢查例如:
var hmac = this.Amazon.CreateHMAC("GET\nwebservices.amazon.com\n/onca/xml\nAWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReviews&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-06", "HMAC_SHA256", "1234567890");
var encoded = WebUtility.UrlEncode(hmac);
Assert.AreEqual("Nace%2BU3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg%3D", encoded);
和實際的結果是:
M%2fy0%2bEAFFGaUAp4bWv%2fWEuXYah99pVsxvqtAuC8YN7I%3d
有任何其他人成功創建HMAC在WinRT上?或者你能看到我在做什麼錯了嗎?