Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate MD5 checksum for a file

Tags:

c#

.net

hash

md5

I'm using iTextSharp to read the text from a PDF file. However, there are times I cannot extract text, because the PDF file is only containing images. I download the same PDF files everyday, and I want to see if the PDF has been modified. If the text and modification date cannot be obtained, is a MD5 checksum the most reliable way to tell if the file has changed?

If it is, some code samples would be appreciated, because I don't have much experience with cryptography.

like image 349
broke Avatar asked May 09 '12 16:05

broke


People also ask

How do you find the checksum of a file?

The checksum is calculated using a hash function and is normally posted along with the download. To verify the integrity of the file, a user calculates the checksum using a checksum calculator program and then compares the two to make sure they match.

What is the checksum of a file?

A checksum is a string of numbers and letters that's used to “check” whether data or a file has been altered during storage or transmission. Checksums often accompany software downloaded from the web so that users can ensure the file or files were not modified in transit.


1 Answers

It's very simple using System.Security.Cryptography.MD5:

using (var md5 = MD5.Create()) {     using (var stream = File.OpenRead(filename))     {         return md5.ComputeHash(stream);     } } 

(I believe that actually the MD5 implementation used doesn't need to be disposed, but I'd probably still do so anyway.)

How you compare the results afterwards is up to you; you can convert the byte array to base64 for example, or compare the bytes directly. (Just be aware that arrays don't override Equals. Using base64 is simpler to get right, but slightly less efficient if you're really only interested in comparing the hashes.)

If you need to represent the hash as a string, you could convert it to hex using BitConverter:

static string CalculateMD5(string filename) {     using (var md5 = MD5.Create())     {         using (var stream = File.OpenRead(filename))         {             var hash = md5.ComputeHash(stream);             return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();         }     } } 
like image 86
Jon Skeet Avatar answered Oct 08 '22 21:10

Jon Skeet