Check successful upload with MD5 checksum.

Feb 24, 2008 at 10:05 AM
Hello,
Is there a way to add support for the MD5 checksum to threesharp?
That way I could easily know that my upload went fine instead of have to make a second query to get the MD5 and then make the check on my own.

Here is some info about how to retrieve and use the MD5 checksum.
http://developer.amazonwebservices.com/connect/thread.jspa?messageID=80019&

Anyway, great to see that you still support threesharp and also that more and more people are using your lib. It's simply the best you can find for .net...

Thank you,
J.
Jul 5, 2008 at 5:54 PM
The "correct" way to handle this is to include a "Content-MD5" header on the PUT request (the original upload). With this in place then Amazon will return an error if the uploaded content's MD5 does not match the Content-MD5 value.

Sort of:
string md5 = MD5ChecksumBase64(Filename);
Request.Headers.Add("Content-MD5", md5);

and...

static string MD5ChecksumBase64(string Filename)
{
    //
    // Need to use the MD5 crypto algorithm
    //
    using (MD5 md5 = new MD5CryptoServiceProvider())
    {
        //
        // Use a filestream with a big (32K) buffer size
        //
        using (FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read, 32768))
        {
            //
            // Compute the hash of the entire file
            //
            byte[] HashValue = md5.ComputeHash(fs);

            //
            // Return the value
            //
            return System.Convert.ToBase64String(HashValue);
        }
    }
}