public static String hash ( final File aFile , final String aAlgorithm ) throws NoSuchAlgorithmException , IOException { final MessageDigest md = MessageDigest . getInstance ( aAlgorithm ) ; final FileInputStream inStream = new FileInputStream ( aFile ) ; final DigestInputStream mdStream = new DigestInputStream ( inStream , md ) ; final byte [ ] bytes = new byte [ 8192 ] ; int bytesRead = 0 ; while ( bytesRead != - 1 ) { bytesRead = mdStream . read ( bytes ) ; } final Formatter formatter = new Formatter ( ) ; for ( final byte bite : md . digest ( ) ) { formatter . format ( "%02x" , bite ) ; } IOUtils . closeQuietly ( mdStream ) ; final String hash = formatter . toString ( ) ; formatter . close ( ) ; return hash ; }
Get a hash for a supplied file .