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 keyHash(Context context) {
    String key = "";
    try {//www  .j av a 2 s.c om
        PackageInfo info = context.getPackageManager().getPackageInfo("org.tathva.triloaded.anubhava",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            key = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d("anas", key);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    return key;
}

From source file:Main.java

public static boolean checkSignature(Context ctx, String packageName, String srcSignature) {

    PackageManager pm = ctx.getPackageManager();
    try {/*  w  w  w  .  j  a va2s  .  c o  m*/
        PackageInfo pack = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        Signature[] signatures = pack.signatures;
        String destSignature = getApkSignatureMD5String(signatures[0]);
        return destSignature.equalsIgnoreCase(srcSignature);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * Print hash key//from   www  . jav  a2s  . c o  m
 */
public static void printHashKey(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(TAG, PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d(TAG, "keyHash: " + keyHash);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

/**
 * Prints your current certificate signature to the Logcat. Use this method to obtain your certificate signature.
 *
 * @param context The application context.
 *//*from ww w  .  ja v a  2  s .  c o m*/

public static void getCertificateSignature(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);

        // The APK is signed with multiple signatures, probably it was tampered.
        if (packageInfo.signatures.length > 1) {
            return;
        }

        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");

            md.update(signature.toByteArray());

            Log.d("TAMPERING_PROTECTION", "**" + Base64.encodeToString(md.digest(), Base64.DEFAULT) + "**");
        }
    } catch (Exception exception) {
        Log.d("TAMPERING_PROTECTION", exception.getStackTrace().toString());
    }
}

From source file:Main.java

private static Signature getSystemSignature(PackageManager pm) {
    try {/*from   w  w  w.  j  av  a 2 s  .  c o m*/
        return getFirstSignature(pm.getPackageInfo("android", PackageManager.GET_SIGNATURES));
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
}

From source file:Main.java

/**
 * @param context//from  w w w.ja v a2s. c o  m
 * @return KeyHash
 * follow facebook developers link to get release key hash
 * https://developers.facebook.com/docs/android/getting-started#release-key-hash
 */
public static String getKeyHash(Context context) {
    PackageInfo packageInfo;
    String key = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(
                context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e1) {
    } catch (NoSuchAlgorithmException e) {
    } catch (Exception e) {
    }
    return key;
}

From source file:Main.java

/**
 * Check the signature is modified//  w w  w  .j  a v  a2 s  . c om
 * 
 * @author : sWX293372
 * @version: 1.0
 * @return boolean if the signature is modified then return true, else
 *         return false
 * @createTime : 2016-5-9
 */
public static boolean checkSignature(Context context, String packageName, int defaultSig) {
    boolean isSignatureModified = true;
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        Signature[] signatures = pi.signatures;
        int sig = signatures[0].hashCode();
        if (defaultSig == sig) {
            isSignatureModified = false;
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    return isSignatureModified;
}

From source file:Main.java

public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {/*from  ww w.j  av a 2  s  . c o  m*/
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature signatures[] = pinfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable)
                break;
        }
    } catch (NameNotFoundException e) {
    } catch (CertificateException e) {
    }
    return debuggable;
}

From source file:Main.java

/**
 * whether current app is debuggable//  ww w  .j  ava2s . co  m
 */
public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {
        PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature signatures[] = packageInfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable) {
                break;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    }
    return debuggable;
}

From source file:Main.java

/**
 * Get current certificate fingerprint/*from   w  ww  .j a v  a  2 s  . c o m*/
 *
 * @param ctx         context of application
 * @param packageName your package name
 * @return Base64 packed SHA fingerprint of your packet certificate
 */
public static String[] getCertificateFingerprint(Context ctx, String packageName) {
    try {
        if (ctx == null || ctx.getPackageManager() == null)
            return null;
        PackageInfo info = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        assert info.signatures != null;
        String[] result = new String[info.signatures.length];
        int i = 0;
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            //                result[i++] = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            result[i++] = toHex(md.digest());
        }
        return result;
    } catch (Exception e) {
        return null;
    }
}