@Test public void testComputeFileHash() throws Exception { File data = getResourceFile(TEST_FILE_PATH); byte[] hash = computeFileHash(data, DEFAULT_OFTP_HASH_ALGORITHM); assertNotNull("Failed to compute file hash: " + DEFAULT_OFTP_HASH_ALGORITHM, hash); assertEquals(DEFAULT_OFTP_HASH_ALGORITHM + " : bad digest were produced.", 20, hash.length); }
public static byte[] computeFileHash(File file, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, IOException { installBouncyCastleProviderIfNecessary(); MessageDigest hash = MessageDigest.getInstance(algorithm, BC_PROVIDER); FileChannel fileChannel = (new FileInputStream(file)).getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(COMPUTING_HASH_BUFFER_SIZE); while (fileChannel.read(buf) != -1) { buf.flip(); hash.update(buf); buf.clear(); } try { fileChannel.close(); } catch (IOException e) { } return hash.digest(); }
SecurityUtil { public static byte[] computeFileHash(File file, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, IOException { installBouncyCastleProviderIfNecessary(); MessageDigest hash = MessageDigest.getInstance(algorithm, BC_PROVIDER); FileChannel fileChannel = (new FileInputStream(file)).getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(COMPUTING_HASH_BUFFER_SIZE); while (fileChannel.read(buf) != -1) { buf.flip(); hash.update(buf); buf.clear(); } try { fileChannel.close(); } catch (IOException e) { } return hash.digest(); } }
SecurityUtil { public static byte[] computeFileHash(File file, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, IOException { installBouncyCastleProviderIfNecessary(); MessageDigest hash = MessageDigest.getInstance(algorithm, BC_PROVIDER); FileChannel fileChannel = (new FileInputStream(file)).getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(COMPUTING_HASH_BUFFER_SIZE); while (fileChannel.read(buf) != -1) { buf.flip(); hash.update(buf); buf.clear(); } try { fileChannel.close(); } catch (IOException e) { } return hash.digest(); }  }
SecurityUtil { public static byte[] computeFileHash(File file, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, IOException { installBouncyCastleProviderIfNecessary(); MessageDigest hash = MessageDigest.getInstance(algorithm, BC_PROVIDER); FileChannel fileChannel = (new FileInputStream(file)).getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(COMPUTING_HASH_BUFFER_SIZE); while (fileChannel.read(buf) != -1) { buf.flip(); hash.update(buf); buf.clear(); } try { fileChannel.close(); } catch (IOException e) { } return hash.digest(); }  static KeyStore openKeyStore(File path, char[] password); static PrivateKey getPrivateKey(KeyStore ks, char[] password); static X509Certificate openCertificate(File path); static void installBouncyCastleProviderIfNecessary(); static X509Certificate getCertificateEntry(KeyStore ks); static byte[] computeFileHash(File file, String algorithm); }
SecurityUtil { public static byte[] computeFileHash(File file, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException, IOException { installBouncyCastleProviderIfNecessary(); MessageDigest hash = MessageDigest.getInstance(algorithm, BC_PROVIDER); FileChannel fileChannel = (new FileInputStream(file)).getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(COMPUTING_HASH_BUFFER_SIZE); while (fileChannel.read(buf) != -1) { buf.flip(); hash.update(buf); buf.clear(); } try { fileChannel.close(); } catch (IOException e) { } return hash.digest(); }  static KeyStore openKeyStore(File path, char[] password); static PrivateKey getPrivateKey(KeyStore ks, char[] password); static X509Certificate openCertificate(File path); static void installBouncyCastleProviderIfNecessary(); static X509Certificate getCertificateEntry(KeyStore ks); static byte[] computeFileHash(File file, String algorithm); static final String BC_PROVIDER; static final String DEFAULT_OFTP_HASH_ALGORITHM; }