The MessageDigestForFile class demonstrates the generation of an MD5W digest for a file. The MD5 digest is displayed in hex using the Hex class from the ApacheSW CommonsSW Codec library. The call to MessageDigest.getInstance("MD5") specifies to use the MD5 algorithm. Other common algorithms are MD2 and SHA.

The getDigest() method of MessageDigestForFile has three parameters. The first is the InputStream of the file that we're going to read from. The second parameter is our MessageDigest. The third parameter is the size of the byte array that we'll use to read from the InputStream and update the MessageDigest. The getDigest() method first resets the MessageDigest to be sure that it is fresh. It reads the bytes of the file to update the MessageDigest. We get the resulting digest from the MessageDigest and convert this to a String that we return from the method.