public static String getSignatureFingerprint(Context context) {
        try {
            Signature[] signs = getRawSignature(context);
            if (signs != null && signs.length > 0) {
                byte[] certBytes = signs[0].toByteArray();
                MessageDigest md = MessageDigest.getInstance("SHA1");
                byte[] fingerprintBytes = md.digest(certBytes);
                // 将字节数组转换为十六进制字符串
                StringBuilder fingerprintBuilder = new StringBuilder();
                for (byte b : fingerprintBytes) {
                    String hex = Integer.toHexString(0xFF & b);
                    if (hex.length() == 1) {
                        fingerprintBuilder.append("0");
                    }
                    fingerprintBuilder.append(hex);
                    fingerprintBuilder.append(":");
                }
                // 移除最后的冒号
                String fingerprint = fingerprintBuilder.toString();
                fingerprint = fingerprint.substring(0, fingerprint.length() - 1);
                return fingerprint;
            }
        } catch (NoSuchAlgorithmException e) {
        }
        return "";
    }

 private static Signature[] getRawSignature(Context context) {
        PackageManager pkgMgr = context.getPackageManager();
        try {
            PackageInfo info = pkgMgr.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
            if (info != null) {
                return info.signatures;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
        return null;
    }

还可以通过   Build.FINGERPRINT  获取,但是两种获取的结果完全不一样.

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐