Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
can any one tell me the fastest way to produce md5 hashes, when a filepath is provided to the method. At present I am using an algorithm which is returning hashed string of 32 bytes. What I think that, if I could get such an algorithm, which can provide hashed string of 16 bytes then the process could be much faster. I am a bit new to it, so may be wrong. Please provide any feedback. This is what I am using.(Got this in an article in Code Project itself).

C#
System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                     new System.Security.Cryptography.MD5CryptoServiceProvider();
           try
           {
               oFileStream = GetFileStream(pathName);
               arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
               oFileStream.Close();
               strHashData = System.BitConverter.ToString(arrbytHashValue);
               strHashData = strHashData.Replace("-", "");
           }
           catch (System.Exception ex)
           {
           }



Thanks and Regards
Anurag
Posted
Updated 18-Nov-10 0:35am
v3

1 solution

Hello Anurag,

a MD5 hash function always yields 128 bit which makes for 16 bytes. 16 bytes in hex are 32 characters. A MD5 hash in a string representation cannot be shorter than 32 characters when expressed in hex. Anyhow, the lengthy duration of the caculation in your case is probably due to the file size. The bigger the file the longer it takes to calculate the hash (Did you spot where computeHash is being fed the file input stream?). How big is that file your hashing and how long does it take?

There are other Hash functions that may be more performant but if you can use them or not depends on what your use case is.

Cheers

Manfred
 
Share this answer
 
Comments
@nuraGGupta@ 18-Nov-10 7:23am    
Thanks Manfred, actually I was considering the bytes in c-sharp only, where 32 bytes means 32 characters. And yes, you are correct, the bigger the file size, the longer time it takes. Anyways, thanks for the response.
@nuraGGupta@ 18-Nov-10 7:53am    
I have to do hashing on the hard disk, or tape drive, or usb drive or any storage device. At a time, my program need to do hashing of 200 gb of data on an average, and it is doing at the rate of 10 gb per hour. I want to make it a bit more faster.
Manfred Rudolf Bihy 18-Nov-10 7:56am    
What kind of files are these that get this big and what is your intention of building a MD5 hash on them?
@nuraGGupta@ 18-Nov-10 8:05am    
these can be any files, suppose video files etc, and intention is unknown,as I have never asked the client for whom I am working on this. It may be some type of recovery of data thing
Manfred Rudolf Bihy 18-Nov-10 8:05am    
But there is always one MD5 hash for each file? What do you do after the MD5 hash is generated?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900