I am downloading a file from an http server and have to take into account that at a random point during download, the network connection fails, or the computer crashes. If that happens, I start a resume download using the HTTP "Range:" header.
Since the download must be validated against an MD5 hash, there seems to be no way for me to use the network inputstream after a resume to get the correct hash, since java.security.MessageDigest doesn't seem to have method that basically says "start upating the current md5 hash from this partial md5 hash that I have from the previous download".
I am not very familiar with the innards of md5 - would this be theoretically possible and is there a library that lets me do that?
Computing the md5 hash from the downloaded file would be prohibitvely expensive performance wise.
An MD5 hash is a form of validation. If a single bit in the file is different, the provided hash will not match the one generated from the file you downloaded. This alerts you to an error in transmission or foul play (rare).
You can easily check the MD5 Hash of any file on your Mac, all you need to do is launch the Terminal and type the 'md5' command and point it at the file you wish to check the md5 has for.
You can feed the MD5 with the content of the file you are resuming from prior to feeding it the network stream.
If you implemented MD5 on your own you could save the state along the partially downloaded file and also resume the MD5 calculation from there. For example using this MD5 implementation it should be as simple as saving/loading the com.twmacinta.util.MD5State state
inside com.twmacinta.util.MD5
. Regarding your comment, note that the native implementations is completely optional and it should work in pure Java. Here is a quote from the documentation:
This class will attempt to use a native method to quickly compute checksums when the appropriate native library is available […] If the library is not found, the code will fall back to the default (slower) Java code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With