一、debug模式下的SHA1,发布的版本正确获取SHA1的获取方式:
开发模式使用 debug.keystore,命令为:keytool -list -v -keystore debug.keystore
发布模式使用 apk 对应的 keystore,命令为:keytool -list -v -keystore xxxx.jks, xxxx.jks为应用创建的密钥库和密钥。
二、代码获得(release)
public static 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 (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
注意debug和release,两个的SHA1的值不能相同
网友评论