如何为http请求计算Amazon的MD5哈希值

时间:2013-01-14 21:38:34

标签: hash coldfusion amazon-web-services coldfusion-9

有没有人知道如何计算需要与Amazon的SubmitFeed API一起使用的MD5哈希?我正在使用ColdFusion,每次我计算MD5哈希值时,它都不会与亚马逊计算的匹配。

亚马逊回应此错误:

ContentMD5DoesNotMatch

the Content-MD5 HTTP header you passed for your feed (C7EF1CADB27497B46FCD6F69516F96E0) did not match the Content-MD5 we calculated for your feed (x+8crbJ0l7RvzW9pUW+W4A==)

我正在使用ColdFusion用于散列的内置函数(例如hash(myStr))。我缺少一步吗?

6 个答案:

答案 0 :(得分:2)

public any function EncryptSignature(required string argValue,required string publicKey) hint="I create my own signature that I will matching later." {
    local.filters=StructNew();
    local.filters["F:publicKey"]=arguments.publicKey;
    var jMsg=JavaCast("string",arguments.argValue).getBytes("iso-8859-1");
    var thisSecretKey = getDAO().getSecretKey(local.filters).apiSecretKey;
    var jKey=JavaCast("string",thisSecretKey).getBytes("iso-8859-1");
    var key=createObject("java","javax.crypto.spec.SecretKeySpec");
    var mac=createObject("java","javax.crypto.Mac");
    key=key.init(jKey,"HmacSHA1");
    mac=mac.getInstance(key.getAlgorithm());
    mac.init(key);
    mac.update(jMsg);
    return lCase(binaryEncode(mac.doFinal(),'Hex'));
    //return Encrypt(arguments.argValue,getapiUsersDAO().getSecretKey(arguments.publicKey),'HMAC-SHA1');
    }

答案 1 :(得分:2)

问题是Feed必须有前面的

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

即使将其添加为宣言也是不够的

var memoryStream = new MemoryStream();
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("AmazonEnvelope",
...

如果您使用的是XmlWriter:

using (var xmlWriter = XmlWriter.Create(memoryStream))
{
    doc.WriteTo(xmlWriter);
}

您必须将XDocument保存到文件,然后从文件中获取流。只有在这种情况下,XDocument才会保留声明(Save()和WriteTo()方法的设计行为):

    var memoryStream = new MemoryStream();
    doc.Save(memoryStream);

    var file = Path.GetTempFileName();
    using (var fileStream = File.OpenWrite(file))
    {
        var buffer = memoryStream.GetBuffer();
        fileStream.Write(buffer, 0, (int)memoryStream.Length);
    }

    return File.Open(file, FileMode.Open, FileAccess.Read);

答案 2 :(得分:1)

我检查了this online tool,您只需要以base64编码发送MD5。它目前只是十六进制编码。

恐怕我不知道ColdFusion的做法是什么,也许这个: SHA or MD5 Digests in ColdFusion

答案 3 :(得分:1)

以下是我为此付出的努力:

<cfset getMD5 = ToBase64(binaryDecode(hash(xmlRequest),'hex'))>

并且它与Amazons MD5哈希相匹配。

答案 4 :(得分:1)

这是另一种java方式 在http://www.kba.suche-spezialwerkzeug.de/pdf/MWSDeveloperGuide.pdf

上找到
public static String computeContentMD5HeaderValue(FileInputStream fis)
        throws IOException, NoSuchAlgorithmException {
    DigestInputStream dis = new DigestInputStream(fis,
            MessageDigest.getInstance("MD5"));
    byte[] buffer = new byte[8192];
    while (dis.read(buffer) > 0)
        ;
    String md5Content = new String(
            org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest())
            );
    // Effectively resets the stream to be beginning of the file via a
    // FileChannel.
    fis.getChannel().position(0);
    return md5Content;
}

答案 5 :(得分:0)

这将给你的愿望输出。

<cfset binaryValue = binaryDecode( 'C7EF1CADB27497B46FCD6F69516F96E0', "hex" )>
<cfset base64Value = binaryEncode( binaryValue, "base64" )>
<cfdump var="#base64Value#">