Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate and display file MD5 Hash in a label

Tags:

c#

md5

How can the MD5 Hash of a file be calculated and displayed in a label?

like image 599
NightsEVil Avatar asked Jun 02 '10 17:06

NightsEVil


People also ask

How does Python calculate MD5 of a file?

# Import hashlib library (md5 method is part of it) import hashlib # File to check file_name = 'filename.exe' # Correct original md5 goes here original_md5 = '5d41402abc4b2a76b9719d911017c592' # Open,close, read file and calculate MD5 on its contents with open(file_name, 'rb') as file_to_check: # read contents of the ...

How do you find the hash of a file in Python?

Source Code to Find Hash Hash functions are available in the hashlib module. We loop till the end of the file using a while loop. On reaching the end, we get empty bytes object. In each iteration, we only read 1024 bytes (this value can be changed according to our wish) from the file and update the hashing function.


2 Answers

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();
    }
}
like image 144
Darin Dimitrov Avatar answered Sep 20 '22 09:09

Darin Dimitrov


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.

like image 42
Ralph Caraveo Avatar answered Sep 20 '22 09:09

Ralph Caraveo