public static String calculateChecksum ( final File installedFile ) throws MojoExecutionException { int bufsize = CHECKSUM_BUFFER_SIZE ; byte [ ] buffer = new byte [ bufsize ] ; FileInputStream fis = null ; BufferedInputStream bis = null ; String checksum = null ; try { MessageDigest sha1 = MessageDigest . getInstance ( "SHA-1" ) ; fis = new FileInputStream ( installedFile ) ; sha1 . reset ( ) ; int size = fis . read ( buffer , 0 , bufsize ) ; while ( size >= 0 ) { sha1 . update ( buffer , 0 , size ) ; size = fis . read ( buffer , 0 , bufsize ) ; } checksum = String . format ( "%040x" , new BigInteger ( 1 , sha1 . digest ( ) ) ) ; } catch ( Exception e ) { throw new MojoExecutionException ( "Failed to calculate digest checksum for " + installedFile , e ) ; } finally { IOUtil . close ( bis ) ; IOUtil . close ( fis ) ; } if ( checksum != null ) { return checksum ; } else { throw new MojoExecutionException ( "Failed to calculate digest checksum for " + installedFile ) ; } }
Calculates the SHA1 checksum for a file using a crude method .