get MD5 for package Sign - Android android.content.pm

Android examples for android.content.pm:PackageManager

Description

get MD5 for package Sign

Demo Code

import java.security.MessageDigest;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class Main {

  public static String getSign(Context context, String pkgName) {
    try {/*from w w  w. j a  v  a  2  s  .  c  o m*/
      PackageInfo pis = context.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
      return hexdigest(pis.signatures[0].toByteArray());
    } catch (NameNotFoundException e) {
      throw new RuntimeException("the " + pkgName + "'s application not found");
    }
  }

  public static String hexdigest(byte[] paramArrayOfByte) {
    final char[] hexDigits = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
    try {
      MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
      localMessageDigest.update(paramArrayOfByte);
      byte[] arrayOfByte = localMessageDigest.digest();
      char[] arrayOfChar = new char[32];
      for (int i = 0, j = 0;; i++, j++) {
        if (i >= 16) {
          return new String(arrayOfChar);
        }
        int k = arrayOfByte[i];
        arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
        arrayOfChar[++j] = hexDigits[(k & 0xF)];
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }

}

Related Tutorials