获取android应用或apk的md5指纹方法

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;
    }


2,遍历系统安装的所有应用信息

    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());
        }
    }


3,从apk文件中获取MD5指纹信息

    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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值