I'm uloading files to a ftp and need to make sure that the files are transmitted correctly. For this I'm redownloading the file afterwards and check if the file is the same as the original local file contentwise. To do this I read each file in small chunks and generate MD5 sums over the content.
Even though MD5 is limited in what it can represent I think it is enough in terms of seeing if there is a difference for teh files (usually up to 2 MB in size). Now though when I generated the MD5 for each stream (one being the download stream the other being the lokal file read stream) I got the problem that the MD5 is different for each of the two (despite the first few chunks being identical in terms of the MD5. And the file itself being a zip file being extractable without problems lokally and on the ftp server).
What I would like to know there is: Am I erring in the idea itself? OR am I having an error in my code? OR why are the contents seemingly different?
The calls:
ftpMD5 = GeneriereMD5FuerStream(ftpAnsuchen.GetResponse().GetResponseStream());
lokalMD5 = GeneriereMD5FuerStream((new FileInfo(lokaleDateiPfad)).OpenRead());
if (ftpMD5.Equals(lokalMD5) == false)
{
throw exception "Different";
}
The code for the method:
private string GeneriereMD5FuerStream(Stream leseStream)
{
string md5String = String.Empty;
byte[] leseBuffer = new byte[2048];
int bytesGelesen = 0;
MD5 md5Converter = MD5.Create();
bytesGelesen = leseStream.Read(leseBuffer, 0, leseBuffer.Length);
md5String = BitConverter.ToString(md5Converter.ComputeHash(Encoding.Default.GetBytes(md5String + BitConverter.ToString(md5Converter.ComputeHash(leseBuffer)))));
while (bytesGelesen > 0)
{
bytesGelesen = leseStream.Read(leseBuffer, 0, leseBuffer.Length);
if (bytesGelesen > 0)
{
md5String = BitConverter.ToString(md5Converter.ComputeHash(Encoding.Default.GetBytes(md5String + BitConverter.ToString(md5Converter.ComputeHash(leseBuffer)))));
}
}
return md5String;
}