Skip to content

Commit 1bd0156

Browse files
authored
Merge pull request Blankj#56 from jp1017/patch-1
see 10/09 log
2 parents b2e47b7 + e87fc5b commit 1bd0156

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import android.content.pm.Signature;
1010
import android.graphics.drawable.Drawable;
1111

12+
import java.security.MessageDigest;
13+
import java.security.NoSuchAlgorithmException;
1214
import java.io.BufferedReader;
1315
import java.io.File;
1416
import java.io.IOException;
@@ -30,6 +32,36 @@ public class AppUtils {
3032
private AppUtils() {
3133
throw new UnsupportedOperationException("u can't instantiate me...");
3234
}
35+
36+
37+
/**
38+
* 获取应用的 SHA1 值, 可据此判断高德,百度地图 key 是否正确
39+
* @param context 上下文
40+
* @return 应用的 SHA1 字符串, 比如: 53:FD:54:DC:19:0F:11:AC:B5:22:9E:F1:1A:68:88:1B:8B:E8:54:42
41+
*/
42+
public static String getSHA1(Context context) {
43+
try {
44+
PackageInfo info = context.getPackageManager().getPackageInfo(
45+
context.getPackageName(), PackageManager.GET_SIGNATURES);
46+
byte[] cert = info.signatures[0].toByteArray();
47+
MessageDigest md = MessageDigest.getInstance("SHA1");
48+
byte[] publicKey = md.digest(cert);
49+
StringBuilder hexString = new StringBuilder();
50+
for (byte aPublicKey : publicKey) {
51+
String appendString = Integer.toHexString(0xFF & aPublicKey).toUpperCase();
52+
if (appendString.length() == 1)
53+
hexString.append("0");
54+
hexString.append(appendString);
55+
hexString.append(":");
56+
}
57+
String result = hexString.toString();
58+
return result.substring(0, result.length() - 1);
59+
} catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
60+
e.printStackTrace();
61+
}
62+
return "";
63+
}
64+
3365

3466
/**
3567
* 判断App是否安装
@@ -628,4 +660,4 @@ public static boolean cleanAppData(Context context, File... dirs) {
628660
}
629661
return isSuccess;
630662
}
631-
}
663+
}

0 commit comments

Comments
 (0)