public static String sHA1(Context context){
try {
@SuppressLint("PackageManagerGetSignatures")
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 b : publicKey) {
String appendString = Integer.toHexString(0xFF & b)
.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 | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
Android 获取SHA1
最新推荐文章于 2024-08-20 00:24:37 发布
这段代码用于从Android应用的包信息中提取第一个签名的SHA1哈希值,通过MessageDigest实例化SHA1算法,对签名字节进行摘要处理,然后将结果转换为16进制字符串。
摘要由CSDN通过智能技术生成