Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function returns different MD5 hash each time

Tags:

c#

md5

I have written a function that takes the URL of a file as a parameter and returns the MD5 hash of that file.

The hash generated by this function (in the development environment) matches the hash generated by a third-party tool for the same file.

But when this was deployed in my client's QA environment, this function returns different values each time the function is called.

I did some Google-ing and found that the issue might be because the dev. server is 64bit and the QA server was 32bit. Since I do not have access to my client's servers, I deployed it in another 32bit server and found that the hashes match even in a 32bit server(function works as expected?).

I also had a look at this question.

here is the function I wrote:

public static String GetMD5HashFromFile(String url)
{
    String response = String.Empty;
    HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse())
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] retVal = md5.ComputeHash(aResponse.GetResponseStream());
        response = "MD5:" + BitConverter.ToString(retVal).Replace("-", string.Empty);
    }
    return response;
}

My questions are; what could be the reason for this function to return various values? Is the issue in my function?

like image 696
Awad Maharoof Avatar asked Apr 08 '13 11:04

Awad Maharoof


1 Answers

Try to add logging. So first download the file to string (write it somewhere on disk, for compare it later). Then calculate hash on disk. I had such problem in one of my project, and in that case problems was with proxy server which added something to response. But without having file on disk you cannot compare them.

like image 168
Piotr Stapp Avatar answered Sep 28 '22 17:09

Piotr Stapp