How can the MD5 Hash of a file be calculated and displayed in a label?
+4
A:
Yes it is possible:
label1.Text = GetMD5HashFromFile("somefile.txt");
where the GetMD5HashFromFile
function could look like this:
public static string GetMD5HashFromFile(string filename)
{
using (var md5 = new MD5CryptoServiceProvider())
{
var buffer = md5.ComputeHash(File.ReadAllBytes(filename));
var sb = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
sb.Append(buffer[i].ToString("x2"));
}
return sb.ToString();
}
}
Darin Dimitrov
2010-06-02 17:19:34
and how would you do all this all self contained (i.e no read from other text files) O.o?
NightsEVil
2010-06-02 19:09:16
@NightsEVil, I am not sure I am following your thought...
Darin Dimitrov
2010-06-02 19:21:04
like wouldnt there be a way for the application to generate the md5 hash and display it in a label without it having to read it from a text file?
NightsEVil
2010-06-02 21:59:22
+1
A:
Yes, it's possible. When you calculate the MD5 Hash of a file you just need to take the result and place it in as the text of Label control. No problem there.
Ralph
2010-06-02 17:20:12
Of course, you will need to encode it, in hex/base64/etc.
BlueRaja - Danny Pflughoeft
2010-06-02 17:23:49
and how would you do all this all self contained (i.e no read from other text files) O.o?
NightsEVil
2010-06-02 18:33:52