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