المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : File Byte[] Conversion to String for MD5 Hash Generation



C# Programming
06-19-2009, 05:44 PM
Hello!

I am trying to write an app that will read a file and generate an RFC 1321 compliant MD5 hash.

I have read a number of tutorials online and have been able to generate a compliant MD5 hash from a string, but I need to be able to pass my app a file and say "what is the MD5 hash of this file?".

Apparently I can read the file in to a byte array and then convert this to a string that my MD5 hash generator can read.


So far I have hit 2 problems:

- My byte[] conversion to string is resulting in an incorrect MD5 hash to all the web generators I have tried.
- I cannot read the byte[] of a file over 2 gigabytes (a buffering is needed I'm told?).


Any chance someone could help me out here? Ignore the part where I am setting the textbox.text value this was just for debug!


private void button1_Click(object sender, EventArgs e) {
// Read File byte[] And Convert To String
Byte[] byteArray = File.ReadAllBytes("C:\\ITV-9C-28225-C.xml");
String stringFromByteArray = Convert.ToBase64String(byteArray);

// Return The MD5 Hash Value
textBox2.Text = MD5Hash(stringFromByteArray);
}

// Generate MD5 Hash Value
public static string MD5Hash(string text) {
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Def ault.GetBytes(text))), "-", "");
}



Cheers in advance!