Amazon Services Japan
Amazonマーケットプレイス Web サービス (Amazon MWS) ドキュメント

Deprecation Notice:

Amazon Marketplace Web Service (MWS) will no longer be available after March 31, 2024. All MWS developers must migrate to Selling Partner API (SP-API) to avoid service disruptions. Refer to the Migration Hub for more information.

Amazon MWS ドキュメント

Content-MD5 ヘッダとMD5チェックサム

MD5は、データ内のすべての変更がダイジェストの変化に反映される、高い確信度を持つ任意の長さのデータの128ビット「ダイジェスト」(またはハッシュ)を計算するためのアルゴリズムです。フィードを送信するため、Content-MD5ヘッダを付ける必要があります。Amazon MWSは送信されたフィード内容でMD5チェックサムを計算し、送信者のつけたContent-MD5ヘッダの内容と比較し、受信されたデータの不正検出を行います。Amazon MWSから送信されるレポートの場合はこのプロセスの逆を行います。 レポートとともに送信されたContent-MD5ヘッダでMD5チェックサムを計算し、Amazonが送信したヘッダと比較し、受信したレポートの不正検出を行います。

Amazon MWSにContent-MD5 ヘッダを含むフィードを送信するための基本的なプロセスは以下の通りです。
  1. Amazon MWSにフィードを送信する前に、送信するフィードをディスクに保管します。
  2. ファイルのコンテンツのMD5チェックサムを計算処理し、コンパニオンファイルにそれを保存します。
  3. SubmitFeedリクエストを作成し、保管してあるMD5チェックサムをContent-MD5に設定し、ストリームにファイルのコンテンツを添付します。
  4. リクエストを送信します。

以下の Java コードサンプルは、Amazonへのフィード送信の際のContent-MD5ヘッダの計算方法について説明しています。

/**
 * Calculate content MD5 header values for feeds stored on disk.
 */
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;
}

以下の Java コードサンプルは、ダウンロードされるレポートのMD5チェックサムの計算方法について説明しています。

/**
 * Consume the stream and return its Base-64 encoded MD5 checksum.
 */
public static String computeContentMD5Header(InputStream inputStream) {
    // Consume the stream to compute the MD5 as a side effect.
    DigestInputStream s;
    try {
        s = new DigestInputStream(inputStream,
                                  MessageDigest.getInstance("MD5"));
        // drain the buffer, as the digest is computed as a side-effect
        byte[] buffer = new byte[8192];
        while(s.read(buffer) > 0);
        return new String(
            org.apache.commons.codec.binary.Base64.encodeBase64(
                s.getMessageDigest().digest()),
                "UTF-8");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}