In this example I'll describe how to use apply the
SHA-256 hashing to a file. However, following the example found in
Coursera's cryptography I course, we won't compute the whole file's hash at once; instead, we'll break the file into 1KB blocks (1024 bytes). Then we'll compute the hash of the last block and appended said value to the second to last block. Then again we'll compute the hash of this augmented second to last block and the resulting hash is appended to the third block from the end, and so on..
Update: Hashing this way doesn't make the hash stronger or weaker, but allows us to stream the file while hashing instead of having to wait for the whole operation to complete.
As usual, auxiliary functions such as
string2Hex can be found here.
The basic imports are:
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.security.MessageDigest;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.io.FileUtils;
Also note, we're using
Apache commons library to handle our File operations