Example usage for android.content.pm PackageManager GET_SIGNATURES

List of usage examples for android.content.pm PackageManager GET_SIGNATURES

Introduction

In this page you can find the example usage for android.content.pm PackageManager GET_SIGNATURES.

Prototype

int GET_SIGNATURES

To view the source code for android.content.pm PackageManager GET_SIGNATURES.

Click Source Link

Document

PackageInfo flag: return information about the signatures included in the package.

Usage

From source file:Main.java

public static String getAppSign(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
    for (PackageInfo pkg : packages) {
        if (packageName.equals(pkg.packageName)) {
            return pkg.signatures[0].toCharsString();
        }//w w w .  j  a v a  2  s.c  o  m
    }

    return null;
}

From source file:Main.java

public static List<PackageInfo> getInstalledPackageInfoList(Context context) {
    List<PackageInfo> packageInfoList = getPackageManager(context)
            .getInstalledPackages(PackageManager.GET_SIGNATURES);
    return packageInfoList;
}

From source file:Main.java

public static byte[] getApkSign(Context context, String packageName) {
    byte[] arrayOfByte = null;
    try {//from   w ww.jav  a  2  s .c  o  m
        PackageInfo localPackageInfo = context.getPackageManager().getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES);
        arrayOfByte = localPackageInfo.signatures[0].toByteArray();
    } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
        localNameNotFoundException.printStackTrace();
    }
    return arrayOfByte;
}

From source file:Main.java

public static String getSign(Context context, String pkgName) {
    try {//w  w w  . j a  v a2  s . com
        PackageInfo pis = context.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
        return hexdigest(pis.signatures[0].toByteArray());
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getSign(Context context, String pkgName) {
    try {//ww  w  . j  a  v a  2s .  co m
        PackageInfo pis = context.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
        return hexdigest(pis.signatures[0].toByteArray());
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static void printHashKey(Context context) {
    try {/*from w  w w .j  a va 2 s.  co m*/
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("HASH KEY:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

static String getSHA1CertFingerprint(Context ctx) {
    try {/*  www  .  j a  va  2 s  .  c o  m*/
        Signature[] sigs = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES).signatures;
        if (sigs.length == 0) {
            return "ERROR: NO SIGNATURE.";
        } else if (sigs.length > 1) {
            return "ERROR: MULTIPLE SIGNATURES";
        }
        byte[] digest = MessageDigest.getInstance("SHA1").digest(sigs[0].toByteArray());
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < digest.length; ++i) {
            if (i > 0) {
                hexString.append(":");
            }
            byteToString(hexString, digest[i]);
        }
        return hexString.toString();

    } catch (PackageManager.NameNotFoundException ex) {
        ex.printStackTrace();
        return "(ERROR: package not found)";
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        return "(ERROR: SHA1 algorithm not found)";
    }
}

From source file:Main.java

public static String getSignature(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi = null;/*  w w  w  .  j  av  a2 s  . c  o  m*/
    StringBuilder builder = new StringBuilder();
    try {
        pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        Signature[] signatures = pi.signatures;
        for (Signature signature : signatures) {
            builder.append(getMessageDigest(signature.toByteArray()));
        }
        return builder.toString();
    } catch (Exception e1) {
        e1.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static void printOutMyHashKey(Context context, String packageName) {
    try {/*w w  w . j  a v a2 s  . c om*/
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getHashKey(Context context) {
    // Add code to print out the key hash
    try {//from www .  j ava  2s .  c  om
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            return Base64.encodeToString(md.digest(), Base64.DEFAULT);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
    return null;
}