tags:

views:

46

answers:

3

the output is always a 1kb file. How can I successfully write convert the byte returned by ComputeHash to a file.?

MD5CryptoServiceProvider test = new MD5CryptoServiceProvider();

            using (FileStream fs = new FileStream(@"D:\test.pdf", FileMode.Open, FileAccess.Read))
            {

                byte[] buffer = test.ComputeHash(fs);
                File.WriteAllBytes(@"D:\test3.pdf", buffer);
            }
A: 

The result is always the same because an MD5 hash has always the same length. Also given a hash you cannot get the original value (well you can always try cracking it), that's the purpose of a hash.

Darin Dimitrov
thanks for all your reply. the reason i want to use ComputeHash is to stream large files. After I google, they say computeHash is best for streaming large files, getting the byte and using the byte to write the file. But I don't know how to implement it. can you further help me with my problem? Thanks
FBM
A: 

I'm assuming you mean: I have a 1kb file, how can i go from that 1kb file to a new file?

A hash is not reversible. The closest you can get to "reversing" a hash is looking at a rainbow table.

If you imagine it, lets say that a string of length 100 chars is given to a hash function which returns a char[32]... Your input has 255^100 different possibilities, while your return value has 255^32 possibilities of return. Obviously collisions will occur [# of inputs > # of outputs]

ItzWarty
A: 

I'm not sure I understand fully what you are looking for, but could it be this?

using(MD5CryptoServiceProvider test = new MD5CryptoServiceProvider())
{

    byte[] buffer = test.ComputeHash(File.ReadAllBytes(@"D:\test3.pdf"));
    File.WriteAllBytes(@"D:\test3.dat", buffer); // PDF would be pointless here
}

Remember though, a hash is not reversible and you cannot make anything short of a byte stream from it. If you are looking to be able to reverse security behavior, look at using encryption.

Kyle Rozendo