views:

150

answers:

2

Good morning all,

I'm working on an MD5 file integrity check tool in C#.

How long should it take for a file to be given an MD5 checksum value? For example, if I try to get a 2gb .mpg file, it is taking around 5 mins+ each time. This seems overly long.

Am I just being impatient?

Below is the code I'm running

public string getHash(String @fileLocation)
    {
        FileStream fs = new FileStream(@fileLocation, FileMode.Open);

        HashAlgorithm alg = new HMACMD5();
        byte[] hashValue = alg.ComputeHash(fs);

        string md5Result = "";

        foreach (byte x in hashValue)
        {
            md5Result += x;
        }           

        fs.Close();           

        return md5Result;           
    }

Any suggestions will be appreciated.

Regards

+6  A: 

See this on how to calculate file hash value in a most efficient way. You basically have to wrap FileStream into a BufferedStream and than feed that into HMACMD5.ComputeHash(Stream) overload:

HashAlgorithm hmacMd5 = new HMACMD5();
byte[] hash;

using(Stream fileStream = new FileStream(fileLocation, FileMode.Open))
    using(Stream bufferedStream = new BufferedStream(fileStream, 1200000))
        hash = hmacMd5.ComputeHash(bufferedStream);
Anton Gogolev
Hi Anton, this seems promising however I'm having a few problems trying to implement your solution. Am I right in thinking that BufferedStream wraps FileStream and both are in their own using statements?Also, in your solution, aren't both classes using the same variable 'stream'.. to double check, this is correct?VC# complained about this.Regards
Ric Coles
@Ric: Edited my answer. See if that helps.
Anton Gogolev
@Anton - Wonderful, works perfectly well. Thanks
Ric Coles
Why are you using HMACMD5 instead of MD5? The former is supposed to be used for validation purposes.
Billy ONeal
I agree with @Billy.. The regular MD5 algorithm should have been used here..
Artiom Chilaru
A: 

how can the returned byte[] be use to produce a file? below code doesnt work, it always produce a 1kb of file.

File.WriteAllBytes(@"D:\test3.pdf", hash);
FBM