美文网首页
Android获取应用签名方法

Android获取应用签名方法

作者: 晖仔Milo | 来源:发表于2021-07-28 10:51 被阅读0次
        /**
         * 获取应用签名
         *
         * @param context
         * @param packageName
         * @return
         */
        public static String getRawSignatureStr(Context context, String packageName) {
            try {
                Signature[] signs = getRawSignature(context, packageName);
                String signValidString = getSignValidString(signs[0].toByteArray());
                return signValidString;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static Signature[] getRawSignature(Context context, String packageName) {
            if (packageName == null || packageName.length() == 0) {
                return null;
            }
            try {
                PackageInfo info = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
                if (info != null) {
                    return info.signatures;
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        private static String getSignValidString(byte[] paramArrayOfByte) throws NoSuchAlgorithmException {
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.update(paramArrayOfByte);
            return toHexString(localMessageDigest.digest());
        }
    
        private static String toHexString(byte[] paramArrayOfByte) {
            if (paramArrayOfByte == null) {
                return null;
            }
            StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
            for (int i = 0; ; i++) {
                if (i >= paramArrayOfByte.length) {
                    return localStringBuilder.toString();
                }
                String str = Integer.toString(0xFF & paramArrayOfByte[i], 16);
                if (str.length() == 1) {
                    str = "0" + str;
                }
                localStringBuilder.append(str);
            }
        }
    

    相关文章

      网友评论

          本文标题:Android获取应用签名方法

          本文链接:https://www.haomeiwen.com/subject/fmczmltx.html