0

我正在尝试向亚马逊 MWS 提交提要。问题是亚马逊说我提交的 Md5 哈希与他们的计算不匹配,所以我的请求被拒绝了。

这是我用来计算哈希的代码:

    var
        StringStream: TStringStream;
        MessageDigest: TIdHashMessageDigest5;
        XMLString: string;
    begin
        MessageDigest := TIdHashMessageDigest5.Create;
        StringStream := TStringStream.Create;
        try
            StringStream.WriteString(XMLString);
            HashString := MessageDigest.HashStream(StringStream);

        finally
            StringStream.Free;
            MessageDigest.Free;
        end;
    end;

这是亚马逊的回应:

    <ErrorResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
        <Error>
            <Type>Sender</Type>
            <Code>ContentMD5DoesNotMatch</Code>
            <Message>the Content-MD5 HTTP header you passed for your feed (H3GnH3X3H3auHZ0mGZ8mD4KvE30mEJauHKD6E3GoDqK) did not match the Content-MD5 we calculated for your feed (fSEMn6SCuSia5YVdz/1NqQ==)</Message>
            <Detail />
        </Error>
        <RequestID>9c3e0b2a-c418-41dc-b733-e55ac01343b1</RequestID>
    </ErrorResponse>

计算 base64 编码的 MD5 哈希的正确方法是什么?我的代码有问题吗?

提前谢谢你,问候。

P。

4

1 回答 1

2

改变这个:

StringStream := TStringStream.Create;
try
  StringStream.WriteString(XMLString);

对此:

StringStream := TStringStream.Create(XMLString);

或这个:

StringStream := TStringStream.Create;
try
  StringStream.WriteString(XMLString);
  StringStream.Position := 0;

无论哪种方式,还请记住,在 XE2 中,String它是 unicode 并且TStringStreamTEncoding-aware,因此请确保在散列之前将字符串编码为正确的字符集,例如:

StringStream := TStringStream.Create(XMLString, TEncoding.UTF8);

最后,TIdHash有一个HashString()(and HashStringAsHex()) 方法,因此您不需要TStream手动处理:

HashString := MessageDigest.HashString(XMLString, IndyUTF8Encoding);
于 2013-05-17T15:31:21.877 回答