MD5 { @Nullable public static String md5(@NonNull File file) { try { final MessageDigest md = MessageDigest.getInstance("MD5"); final InputStream inputStream = new DigestInputStream(new FileInputStream(file), md); final byte[] buffer = new byte[1024]; while (inputStream.read(buffer) != -1) { } inputStream.close(); return String.format("%1$032x", new BigInteger(1, md.digest())); } catch (NoSuchAlgorithmException e) { Log.e(TAG, "Failed to compute MD5", e); return null; } catch (IOException e) { Log.e(TAG, "Failed to read file", e); return null; } }  @Nullable static String md5(@NonNull File file);  }
@Test public void emptyFile() throws IOException { File tempfile = File.createTempFile("md5-test-", ".txt"); String checksum = MD5.md5(tempfile); assertTrue(tempfile.delete()); assertEquals("d41d8cd98f00b204e9800998ecf8427e", checksum); }