Here you can find the source of getCertificateFingerprint(Context ctx, String packageName)
Parameter | Description |
---|---|
ctx | context of application |
packageName | your package name |
public static String[] getCertificateFingerprint(Context ctx, String packageName)
//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); } }