How can i calculate the Md5 of File

Hi experts

I wanted to know how to calculate the MD5 of a file

Please Help
Pseudo code on wiki
http://en.wikipedia.org/wiki/MD5
I don't know how they hash files -- the only way I can think of doing it is to hash the entire contents of the file, but for very large files that won't work: you'll run out of memory (and it would take forever to hash). The only way you could handle large files would be to hash portions at a time and then re-hash the previous hashes, but that would be inconsistent -- with a 4 GiB file, if one MD5 program hashes it in 512 kiB chunks and another in 1024 kiB chunks, you'll end up with different hashes.

Hashing the filename also doesn't work -- it makes the whole concept of hashing files redundant. Hashing the inode number also doesn't work because when you download a file the inode number only has a 1 in n chance of being correct (where n is the number of inodes on the filesystem; which would likely be different for different filesystems). The only logical way of hashing a file would be to hash the contents, but like I said, it's inconsistent between implementations...
Last edited on
Use lib OpenSSL for hashing. Unless you want to implement it yourself.
closed account (z05DSL3A)
Crypto++ is also worth a look:
http://www.cryptopp.com/
The md5 hash is a stream hash. So you feed it chunks of bytes until you are happy then finish the calculation.

It would be rather pointless to have a checksum that is implementation based since that would limit the usability and thus would not be so usable.
The issue is how large the chunks are. If two implementations use different chunks, as in my example, then the hash would be useless.
Topic archived. No new replies allowed.