MCPcopy Create free account
hub / github.com/kk121/File-Loader / MD5

Class MD5

fileloader/src/main/java/com/krishna/fileloader/utility/MD5.java:15–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13import java.security.NoSuchAlgorithmException;
14
15public class MD5 {
16 private static final String TAG = "MD5";
17
18 public static boolean checkMD5(String md5, File updateFile) {
19 if (TextUtils.isEmpty(md5) || updateFile == null) {
20 Log.e(TAG, "MD5 string empty or updateFile null");
21 return false;
22 }
23
24 String calculatedDigest = calculateMD5(updateFile);
25 if (calculatedDigest == null) {
26 Log.e(TAG, "calculatedDigest null");
27 return false;
28 }
29
30 Log.v(TAG, "Calculated digest: " + calculatedDigest);
31 Log.v(TAG, "Provided digest: " + md5);
32
33 return calculatedDigest.equalsIgnoreCase(md5);
34 }
35
36 public static String calculateMD5(File updateFile) {
37 MessageDigest digest;
38 try {
39 digest = MessageDigest.getInstance("MD5");
40 } catch (NoSuchAlgorithmException e) {
41 Log.e(TAG, "Exception while getting digest", e);
42 return null;
43 }
44
45 InputStream is;
46 try {
47 is = new FileInputStream(updateFile);
48 } catch (FileNotFoundException e) {
49 Log.e(TAG, "Exception while getting FileInputStream", e);
50 return null;
51 }
52
53 byte[] buffer = new byte[8192];
54 int read;
55 try {
56 while ((read = is.read(buffer)) > 0) {
57 digest.update(buffer, 0, read);
58 }
59 byte[] md5sum = digest.digest();
60 BigInteger bigInt = new BigInteger(1, md5sum);
61 String output = bigInt.toString(16);
62 // Fill to 32 chars
63 output = String.format("%32s", output).replace(' ', '0');
64 return output;
65 } catch (IOException e) {
66 throw new RuntimeException("Unable to process file for MD5", e);
67 } finally {
68 try {
69 is.close();
70 } catch (IOException e) {
71 Log.e(TAG, "Exception on closing MD5 input stream", e);
72 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected