Android Context Get getCertificateFingerprint(Context ctx, String packageName)

Here you can find the source of getCertificateFingerprint(Context ctx, String packageName)

Description

Get current certificate fingerprint

License

Open Source License

Parameter

Parameter Description
ctx context of application
packageName your package name

Return

Base64 packed SHA fingerprint of your packet certificate

Declaration

public static String[] getCertificateFingerprint(Context ctx,
        String packageName) 

Method Source Code

//package com.java2s;
//  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of

import android.content.Context;

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

import android.content.pm.Signature;

import java.math.BigInteger;
import java.security.MessageDigest;

public class Main {
    /**/*from   ww w  .j a  v  a  2 s.  c om*/
     * Get current certificate fingerprint
     *
     * @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;
        }
    }

    private static String toHex(byte[] bytes) {
        BigInteger bi = new BigInteger(1, bytes);
        return String.format("%0" + (bytes.length << 1) + "X", bi);
    }
}

Related

  1. getAuthorityFromPermission(Context context, String permission)
  2. getBatteryUsageInfo(Context context)
  3. getBoolean(Context context, String key, boolean defValue)
  4. getBroadcastAddress(Context context)
  5. getCarrier(Context context)
  6. getCertificateFromSDCard(Context context)
  7. getChannelNo(Context context)
  8. getConnectedType(Context context)
  9. getConnectedType(Context context)