C# - 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

This question and answers originated from www.stackoverflow.com
Question by (8/28/2009 9:08:11 AM)

Answer

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

Find More Answers
Related Topics  c#  hash  md5  integrity
Related Questions
  • Faster MD5 alternative?

    I'm working on a program that searches entire drives for a given file. At the moment, I calculate an MD5 hash for the known file and then scan all files recursively, looking for a match. The only…
  • C# MD5 hasher example

    Edit: I've retitled this to an example as the code works as expected. I am trying to copy a file, get a MD5 hash, then delete the copy. I am doing this to avoid process locks on the original file…
  • C#: String -> MD5 -> Hex

    in languages like PHP or Python there are convenient functions to turn an input string into an output string that is the HEXed representation of it. I find it a very common and useful task (passw…
  • about MD5 checksum for Http big file downloading

    MD5 checksum is widely used for integrity checking for Http downloading big files. My question is, since TCP itself provides reliable mechanism (i.e. checksum for each TCP package to ensure its inte…
  • md5 hash for file without File Attributes

    Using the following code to compute MD5 hashs of files: Private _MD5Hash As String Dim _BinaryData As Byte() = New Byte(FileUpload1.PostedFile.InputStream.Length) {} FileUpload1.PostedFile.Inp…
  • MD5 file integrity check

    I know that hash functions make calculations, and the final result is string with fixed length. They are used to verify that files are the same - downloaded files, for example. There is hash present…
  • MD5 File Hashing - match Delphi output with PHP md5_file function

    I'm currently using this code for md5 hashing in Delphi 7: function MD5(const fileName : string) : string; var idmd5 : TIdHashMessageDigest5; fs : TFileStream; begin idmd5 := TIdHashMessageDig…
  • C# calculate MD5 for opened file?

    how I can calculate MD5 hash for a file that is open or used by a process? the files can be txt or and exe my current code return error for an exe because it is running here is my current c…
  • C#: How to generate short MD5 code?

    When I am encrypting 23 using MD5 encryption I am getting 37693cfc748049e45d87b8c7d8b9aacd this 32-character long string which will always be static for 23. I want the same kind of mechanism but …
  • C# MD5 Hash results not expected result

    I've tried every example I can find on the web but I cannot get my .NET code to produce the same MD5 Hash results from my VB6 app. The VB6 app produces identical results to this site: http://www…