Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello! As some of you already migth know, I am currently working on a software taht compares MD5 Hashes, my question is how do I make my code read through the file I am directing it to?

C#
        public string MD5HashFile(string fn)
        {
            byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
            return BitConverter.ToString(hash).Replace("-", "");

        }


private Stream TestStream()
{
    Stream fs = File.OpenRead(@"C:\PathToDictionary");
    return fs;
}

public string GetMD5(string file)
{
    using (var md5 = MD5.Create())
    using (var stream = File.OpenRead(TestStream))
    return Encoding.Default.GetString(md5.ComputeHash(stream));
}



This is my current code and as you probably already figured out, its throwing me an error.

To be more specific its telling me that

My TestStream() method already returns a Stream and File.OpenRead is for creating a new Stream which is not needed here because TestStream() is already a Stream. I can replace the using(var stream=..... line by var stream = TestStream()

But... When I do that its still throwing me the same error so what did I miss here? And dont go easy on me, if there is something obvious that I missed please feel free to tell me!

What I have tried:

Ive tried replace the using(var stream=..... line with var stream = TestStream()
Posted
Updated 8-May-16 9:01am
v2
Comments
BillWoodruff 8-May-16 12:19pm    
There are so many good examples of reading a stream available that I find it strange you are asking about this. Just do a search.
BladeLogan 8-May-16 12:21pm    
I've been looking around, is there any other method you could recommend for me? instead of the one that I am using?
BladeLogan 8-May-16 15:02pm    
also, I updates my question a bit.

Why not just say either:
C#
string s = File.ReadAllText(pathToFile);

or
C#
byte[] rawData = File.ReadAllBytes(pathToFile);
For MD5, I'd use the later and generate an MD5 of the raw byte data instead of playing with strings.

"This sounds like the easier solution, and I like things to be cleana nd simple, but how would I make the code compare the hash file that I compute with the one from my text file? I have a text file with over 26k hashes that is going to be compares, so my goal is to compare the hash that I compute with the hashes from the text file, do you understand?"


There are a load of different ways to do that, but what I'd probably do is use a Dictionary for starters.
Because there can only be one hash value for any one file, I'd store the data in the file in pairs, separated by a character illegal in file names: '?' for example.

path/to/file?hashValue
path/to/other/file?otherHashValue


Then I'd read the file using File.ReadAllLines - that gives you an array, with each pair in a separate string.
Use Split to "break" the pairs, and add them to a Dictionary<string, string> so that the Key was the path to the file (since this has to be unique) and the MD5 is the Value.
Then when you generate a MD5 for comparision it's pretty trivial:
C#
if (myDictionaryOfFilesAndHashes[pathToFile] == calculatedMD5HashValueAsAString)
    {
    ...
    }

Make sense?
 
Share this answer
 
v2
Comments
BladeLogan 8-May-16 14:59pm    
This sounds like the easier solution, and I like things to be cleana nd simple, but how would I make the code compare the hash file that I compute with the one from my text file? I have a text file with over 26k hashes that is going to be compares, so my goal is to compare the hash that I compute with the hashes from the text file, do you understand?
For reading a file, better use System.IO.StreamReader:
StreamReader Class (System.IO)[^].

Don't forget to create it's instance under the using statement: using Statement (C# Reference)[^].

Your real problem is reading MD5. You did not say anything about the format the hash is stored in, so I cannot say how it can really be read. All you need is to store the bytes of the hash. If you really want to use the text file, it should be not bytes, but some kind of string representation of bytes, say, in hexadecimal form. In this approach, you should read, say, the whole string and then try to parse it to the sequence of bytes. The problem is very simple, only you should not call ComputeHash in a blind-folded manner. If you have more detail on how the hash value is stored but are not sure what exactly to do, I'll probably will be able to help you with your next step.

Alternatively, use a "binary" file.

Also, don't forget that MD5 is not suitable for cryptography and security-sensitive application; it was found broken:
MD5 — Wikipedia, the free encyclopedia[^],
see also: Cryptographic hash function — Wikipedia, the free encyclopedia[^],
HashAlgorithm Class (System.Security.Cryptography)[^].

—SA
 
Share this answer
 
v2
Comments
ridoy 8-May-16 13:17pm    
good suggestions, a 5.
Sergey Alexandrovich Kryukov 8-May-16 13:18pm    
Thank you very much.
—SA
BladeLogan 8-May-16 15:02pm    
Thank you for explaining! updated my question a bit aswell.
Sergey Alexandrovich Kryukov 8-May-16 18:05pm    
You are welcome.
If you get an exception, provide comprehensive expression information. If you merely read some files, prefer using stream readers; there are readers oriented to text data and binary.
—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