public static string CalculateMD5(Stream stream, int bufferSize)
{
MD5 md5Hasher = MD5.Create();
byte[] buffer = new byte[bufferSize];
int readBytes;
while ((readBytes = stream.Read(buffer, 0, bufferSize)) > 0)
{
md5Hasher.TransformBlock(buffer, 0, readBytes, buffer, 0);
}
md5Hasher.TransformFinalBlock(new byte[0], 0, 0);
var result = md5Hasher.Hash.Aggregate(string.Empty, (full, next) => full + next.ToString("x2"));
return result;
}