高德地图开发者平台获取sHA1值

一般在  Application 中进行初始化

 

 1 /**
 2      * 获取高德SHA1值
 3      * */
 4 
 5     public static String sHA1(Context context) {
 6         try {
 7             PackageInfo info = context.getPackageManager().getPackageInfo(
 8                     context.getPackageName(), PackageManager.GET_SIGNATURES);
 9             byte[] cert = info.signatures[0].toByteArray();
10             MessageDigest md = MessageDigest.getInstance("SHA1");
11             byte[] publicKey = md.digest(cert);
12             StringBuffer hexString = new StringBuffer();
13             for (int i = 0; i < publicKey.length; i++) {
14                 String appendString = Integer.toHexString(0xFF & publicKey[i])
15                         .toUpperCase(Locale.US);
16                 if (appendString.length() == 1)
17                     hexString.append("0");
18                 hexString.append(appendString);
19                 hexString.append(":");
20             }
21             String result = hexString.toString();
22             Log.i("SHA11---",result);
23             Log.i("SHA22---",result.substring(0,result.length()-1));
24             return result.substring(0, result.length()-1);
25         } catch (PackageManager.NameNotFoundException e) {
26             e.printStackTrace();
27         } catch (NoSuchAlgorithmException e) {
28             e.printStackTrace();
29         }
30         return null;
31     }

 

posted @ 2018-06-08 11:41  monkey0928  阅读(571)  评论(0编辑  收藏  举报