GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — getSHA1 · RlonLL11FE/AndroidUtilCode@e87fc5b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • utilcode/src/main/java/com/blankj/utilcode/utils
    • AppUtils.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java
+33-1Lines changed: 33 additions & 1 deletion
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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.