android SHA1 一个方法获取简单直接(wings6)

要用到一些第三方需要配置SHA1 下面写上获取方法

正式环境
测试环境

两个不一样的 有时候不生效就是配错了 在第三方配置的时候 记得区分开

// 获取现在运行版本的SHA1 
public static String SHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
            }
            String MySha1String = hexString.toString();
            String ReSHA1 = "";
            int IntOne = 0;
            int IntTwo = 2;
            for (int i = 0; i < (MySha1String.length() / 2); i++) {
                String substring = MySha1String.substring(IntOne, IntTwo);
                ReSHA1 = ReSHA1 + substring + ":";
                IntOne = IntOne + 2;
                IntTwo = IntTwo + 2;
            }
            ReSHA1 = ReSHA1.substring(0, ReSHA1.length() - 1);
            Log.d("SHA1", ReSHA1);//打印SHA1 直接复制配置一下就好 
            return hexString.toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(android SHA1 一个方法获取简单直接(wings6))