Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. I'm a newbie. I came across this on the Web. Basically u need to compare and test the general performance of hash algorithms i.e MD5 and SHA1 using .NET Framework 4.0.
Here's how it goes:
1. Write a a method that accepts the path to a file as a parameter.
2. A stream is opened to read the file and compute the hash.
3. The result of the hash is converted to a string via a BitConverter.
4. The method was cloned for each algorithm being tested: MD5 & SHA1
5. Each algorithm was tested against a 50K, 100K, 500K, 1MB, and 5MB file.
6. The checksum was calculated for the given file 1000 times to obtain a reasonable
measurement.
7. The test to be compiled in Release Mode on .NET Framework 4.0

So, I'm wondering whether the above test/program is feasible, practical & realistic and how to go about executing it.

Appreciate it if anyone can shed some light on this.

Many thanks.
Giggsey
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 14:53pm    
What are the problems? Why BitConverter?
--SA

If you are new int programming, this is not the place to start. Beyond that, I'm not sure I really see the point. However, if you wanted to do something like this, there is an open source application that does this. You could look at the code to see how and see what else they do:

http://hashlib.codeplex.com/[^]

If you want to attemp this yourself, yes it is feasible. I really don't see a high level of practicality, but it is mostly realistic. As far as how to actually write this code, I think you have a good start already. You've got the application broken down into logical pieces. Break these pieces down further into individual methods. Then, implement each method. That way, when you get stuck, it will be on a small piece, not an entire application. That will make getting help easier.
 
Share this answer
 
Comments
Mehdi Gholam 28-Jun-12 15:05pm    
My 5! great resource!
Sergey Alexandrovich Kryukov 28-Jun-12 15:17pm    
Why? Everything OP needs is available in .NET and described in MSDN; if you see my answer... :-)
--SA
Giggsey73 29-Jun-12 4:00am    
Thanks. Appreciate it.
This is not just feasible, this is trivial with .NET.

You did not explain how do you want to use MD5 and SHA1 except performance testing, so I want to start with the following warning: both MD5 and SHA1 were developer as the cryptographic hash functions, but both were found broken. None of these functions should be used for cryptographic or security-related purposes. Please see:
http://en.wikipedia.org/wiki/Cryptographic_hash_function[^],
http://en.wikipedia.org/wiki/MD5[^],
http://en.wikipedia.org/wiki/SHA-1[^].

I would advise to use the cryptographic hash functions of the SHA-2 family. Please see:
http://en.wikipedia.org/wiki/SHA2[^].

Now, the implementations are readily available with .NET as they are bundled with each version of the Framework and placed in GAC. Please see:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx[^].

So, main part of your problem is solved. It is not clear why do you need to use BitConverter. You really never need it for this task. All algorithms work with arrays of bytes, on input and on output. To produce some human-readable text, you can output each byte using byte.ToString and read using byte.Parse or byte.TryParse. You can also store or load any data structure (actually, any arbitrary object graph with objects with any arbitrary data structures) in an XML file, preferably using Data Contract. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Now, for reading arbitrary data files to be hashed (probably, you only need reading), you need to read then as binary data, which you can do with the class System.IO.BinaryReader:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readbytes.aspx[^] (this is the main method you should use).

The method shown above will require you to know the file size, to read all at once. This is done by getting the base stream of a reader as a value of the property BaseStream, and getting its length using its property System.IO.Stream.Length. This property is not always implemented, but for an instance of an input stream obtained from the reader, it will return correct value. Please see:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.basestream.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.stream.aspx[^].

[EDIT]

Another, easier way of reading all bytes from any file is the method System.IO.File.ReadAllBytes:
http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx[^].

[END EDIT]

And, finally, to get most precise timing, you need to use the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

This is how you can assess what accuracy you can rely upon:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency.aspx[^].

See also on timing:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsed.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

This is all you need to know. Everything else is too trivial to discuss, but, if you face any problems, you are welcome to ask a follow-up question.

—SA
 
Share this answer
 
v4
Comments
Mehdi Gholam 28-Jun-12 15:41pm    
Comprehensive as always, 5!
Sergey Alexandrovich Kryukov 28-Jun-12 16:51pm    
Thank you, Mehdi.
--SA
Giggsey73 29-Jun-12 3:57am    
Thanks. Appreciate it.
Sergey Alexandrovich Kryukov 29-Jun-12 17:56pm    
You are welcome.
Will you accept it formally then (green button)? -- thanks.
--SA

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