Md5 sum : MD5.xml « Security « Android






Md5 sum

 
//com.pommedeterresautee.twoborange.System;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class Md5sum {

  private static final String TAG = "MD5";

  public static boolean checkMD5(String md5, File updateFile) {
    if (md5 == null || md5.equals("") || updateFile == null) {
      Log.e(TAG, "md5 String NULL or UpdateFile NULL");
      return false;
    }

    String calculatedDigest = calculateMD5(updateFile);

    if (calculatedDigest == null) {
      Log.e(TAG, "calculatedDigest NULL");
      return false;
    }

    Log.i(TAG, "Calculated digest: " + calculatedDigest);
    Log.i(TAG, "Provided digest: " + md5);

    return calculatedDigest.equalsIgnoreCase(md5);
  }

  public static String calculateMD5(File updateFile) {
    MessageDigest digest;
    try {
      digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      Log.e(TAG, "Exception while getting Digest", e);
      return null;
    }
    InputStream is;
    try {
      is = new FileInputStream(updateFile);
    } catch (FileNotFoundException e) {
      Log.e(TAG, "Exception while getting FileInputStream", e);
      return null;
    }
    byte[] buffer = new byte[8192];
    int read;
    try {
      while ((read = is.read(buffer)) > 0) {
        digest.update(buffer, 0, read);
      }
      byte[] md5sum = digest.digest();
      BigInteger bigInt = new BigInteger(1, md5sum);
      String output = bigInt.toString(16);
      // Fill to 32 chars
      output = String.format("%32s", output).replace(' ', '0');
      return output;
    } catch (IOException e) {
      throw new RuntimeException("Unable to process file for MD5", e);
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        Log.e(TAG, "Exception on closing MD5 input stream", e);
      }
    }
  }

  public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {
      Process p = Runtime.getRuntime().exec("su");
      OutputStream os = p.getOutputStream();
      os.write(("md5sum " + recoveryFilename).getBytes());
      os.flush();
      os.close();
      InputStream is = p.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      String str = br.readLine();
      MD5string = str.split("  ")[0].trim();
      is.close();
      br.close();
      p.destroy();
    } catch (Exception e) {
      Log.e(TAG, "Exception on getting Recovery MD5", e);
      return null;
    }
    Log.i(TAG, "Recovery MD5: " + MD5string);
    return MD5string;
  }
}

   
  








Related examples in the same category

1.MD5 hash generator.
2.md5 sum and hmac Sha1 Digest
3.Create Md5 Checksum
4.MD5 Hash
5.MD5 String Util
6.Download from URL and MD5 check
7.Md5 Hash and MessageDigest
8.Md5 String