当前位置:网站首页>App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value

App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value

2022-06-23 05:52:00 Muzi 102

When integrating maps in a project, you often need to go to SHA1 This value is used to configure key The following is a simple acquisition app SHA1 Value program

    /** *  obtain sha1 * @param context * @return */
    public 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);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length() - 1);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
原网站

版权声明
本文为[Muzi 102]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230414189241.html