Ric
Ric

Reputation: 693

MD5 file processing

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

Upvotes: 1

Views: 2948

Answers (1)

Anton Gogolev
Anton Gogolev

Reputation: 115741

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);

Upvotes: 7

Related Questions