New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSHA1 #56

Merged
merged 1 commit into from Oct 9, 2016
Jump to file or symbol
Failed to load files and symbols.
+33 −1
Diff settings

Always

Just for now

@@ -9,6 +9,8 @@
import android.content.pm.Signature;
import android.graphics.drawable.Drawable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
@@ -30,6 +32,36 @@
private AppUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 获取应用的 SHA1 值, 可据此判断高德,百度地图 key 是否正确
* @param context 上下文
* @return 应用的 SHA1 字符串, 比如: 53:FD:54:DC:19:0F:11:AC:B5:22:9E:F1:1A:68:88:1B:8B:E8:54:42
*/
public static String getSHA1(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);
StringBuilder hexString = new StringBuilder();
for (byte aPublicKey : publicKey) {
String appendString = Integer.toHexString(0xFF & aPublicKey).toUpperCase();
if (appendString.length() == 1)
hexString.append("0");
hexString.append(appendString);
hexString.append(":");
}
String result = hexString.toString();
return result.substring(0, result.length() - 1);
} catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
/**
* 判断App是否安装
@@ -628,4 +660,4 @@ public static boolean cleanAppData(Context context, File... dirs) {
}
return isSuccess;
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.