title: Android Android 获取应用签名证书的SHA1值和MD5值几种方法 date: 2020-04-13 14:58:56 tags: android
keytool -printcert -file CERT.RSA
在jks签名文件目录下打开cmd(按住Shift,点击鼠标右键),输入命令 : keytool -list -v -keystore xxx.jks ,就可以在CMD命令窗口中看到签名文件的信息了,其中包括了SHA1值和MD5值。
keytool -list -v -keystore xxx.jks
/** * 获取签名工具类 public class AppSigning { public final static String MD5 = "MD5"; public final static String SHA1 = "SHA1"; public final static String SHA256 = "SHA256"; private static HashMap<String, ArrayList<String>> mSignMap = new HashMap<>(); * 返回一个签名的对应类型的字符串 * @param context * @param type * @return 因为一个安装包可以被多个签名文件签名,所以返回一个签名信息的list public static ArrayList<String> getSignInfo(Context context, String type) { if (context == null || type == null) { return null; String packageName = context.getPackageName(); if (packageName == null) { return null; if (mSignMap.get(type) != null) { return mSignMap.get(type); ArrayList<String> mList = new ArrayList<String>(); try { Signature[] signs = getSignatures(context, packageName); for (Signature sig : signs) { String tmp = "error!"; if (MD5.equals(type)) { tmp = getSignatureByteString(sig, MD5); } else if (SHA1.equals(type)) { tmp = getSignatureByteString(sig, SHA1); } else if (SHA256.equals(type)) { tmp = getSignatureByteString(sig, SHA256); mList.add(tmp); } catch (Exception e) { LogUtil.e(e.toString()); mSignMap.put(type, mList); return mList; * 获取签名sha1值 * @param context * @return public static String getSha1(Context context) { String res = ""; ArrayList<String> mlist = getSignInfo(context, SHA1); if (mlist != null && mlist.size() != 0) { res = mlist.get(0); return res; * 获取签名MD5值 * @param context * @return public static String getMD5(Context context) { String res = ""; ArrayList<String> mlist = getSignInfo(context, MD5); if (mlist != null && mlist.size() != 0) { res = mlist.get(0); return res; * 获取签名SHA256值 * @param context * @return public static String getSHA256(Context context) { String res = ""; ArrayList<String> mlist = getSignInfo(context, SHA256); if (mlist != null && mlist.size() != 0) { res = mlist.get(0); return res; * 返回对应包的签名信息 * @param context * @param packageName * @return private static Signature[] getSignatures(Context context, String packageName) { PackageInfo packageInfo = null; try { packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES); return packageInfo.signatures; } catch (Exception e) { LogUtil.e(e.toString()); return null; * 获取相应的类型的字符串(把签名的byte[]信息转换成16进制) * @param sig * @param type * @return private static String getSignatureString(Signature sig, String type) { byte[] hexBytes = sig.toByteArray(); String fingerprint = "error!"; try { MessageDigest digest = MessageDigest.getInstance(type); if (digest != null) { byte[] digestBytes = digest.digest(hexBytes); StringBuilder sb = new StringBuilder(); for (byte digestByte : digestBytes) { sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3)); fingerprint = sb.toString(); } catch (Exception e) { LogUtil.e(e.toString()); return fingerprint; * 获取相应的类型的字符串(把签名的byte[]信息转换成 95:F4:D4:FG 这样的字符串形式) * @param sig * @param type * @return private static String getSignatureByteString(Signature sig, String type) { byte[] hexBytes = sig.toByteArray(); String fingerprint = "error!"; try { MessageDigest digest = MessageDigest.getInstance(type); if (digest != null) { byte[] digestBytes = digest.digest(hexBytes); StringBuilder sb = new StringBuilder(); for (byte digestByte : digestBytes) { sb.append(((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3)).toUpperCase()); sb.append(":"); fingerprint = sb.substring(0, sb.length() - 1).toString();