1,获取当前应用的MD5指纹
/**
* 获取md5指纹信息(签名文件不同,md5指纹也不同;不同包名app可以具有相同的md5指纹信息)
* 示例输出:
* sign: 55:2e:ba:e6:b4:7e:ac:e3:02:58:64:9a:db:82:87:b6
*/
private void checkCertificate(Context context) {
try {
PackageManager pm = context.getPackageManager();
Signature sig = pm.getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES).signatures[0];
String md5Fingerprint = doFingerprint(sig.toByteArray(), "MD5");
Log.d("sign:", md5Fingerprint);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param certificateBytes 获取到应用的signature值
* @param algorithm 在上文指定MD5算法
* @return md5签名
*/
private String doFingerprint(byte[] certificateBytes, String algorithm) throws Exception {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(certificateBytes);
byte[] digest = md.digest();
String toRet = "";
for (int i = 0; i < digest.length; i++) {
if (i != 0) {
toRet += ":";
}
int b = digest[i] & 0xff;
String hex = Integer.toHexString(b);
if (hex.length() == 1) {
toRet += "0";
}
toRet += hex;
}
return toRet;
}
private void getApplistSignture() { //遍历系统安装应用信息
final PackageManager packageManager = getPackageManager();
final List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES);
for (PackageInfo p : packageList) {
StringBuilder sb = new StringBuilder();
final String appName = p.applicationInfo.loadLabel(packageManager).toString();
final String packageName = p.packageName;
sb.append("<br>" + appName + " / " + packageName + "<br>");
final android.content.pm.Signature[] arrSignatures = p.signatures;
for (final Signature sig : arrSignatures) {//通过sig可以计算出指纹信息
/*
* Get the X.509 certificate.
*/
final byte[] rawCert = sig.toByteArray();
InputStream certStream = new ByteArrayInputStream(rawCert);
final CertificateFactory certFactory;
final X509Certificate x509Cert;
try {
certFactory = CertificateFactory.getInstance("X509");
x509Cert = (X509Certificate) certFactory.generateCertificate(certStream);
sb.append("Certificate sig hashcode: " + x509Cert.hashCode() + "<br>");
sb.append("Certificate subject: " + x509Cert.getSubjectDN() + "<br>");
sb.append("Certificate issuer: " + x509Cert.getIssuerDN() + "<br>");
//serial number同md5指纹性质相同,随签名文件变化而变化
sb.append("Certificate serial number: " + x509Cert.getSerialNumber() + "<br>");
sb.append("<br>");
} catch (CertificateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Log.d("log sign", sb.toString());
}
}
private void showUninstallAPKInfor(String apkPath) { //apkPath为文件路径
final PackageManager packageManager = getPackageManager();
PackageInfo packageSign = packageManager.getPackageArchiveInfo(apkPath, PackageManager.GET_SIGNATURES);
if (packageSign != null) {
ApplicationInfo applicationInfo = packageSign.applicationInfo;
boolean debug = (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
if (debug) {
Log.d("log apk", "该文件为测试程序");
} else {
Log.d("log apk", "该文件为发布程序");
}
try {
Log.d("log apk", "apk文件签名信息:" + doFingerprint(packageSign.signatures[0].toByteArray(), "MD5"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
4,获取keystore或jks的指纹信息
keytool -list -v -keystore debug.keystore
5,通过apk文件中的CERT.RSA获取MD5指纹、SHA1、SHA256
keytool -printcert -file CERT.RSA