Android 获取app签名指纹
Android 获取app签名指纹
·
public static String getSignatureFingerprint(Context context) {
try {
Signature[] signs = getRawSignature(context);
if (signs != null && signs.length > 0) {
byte[] certBytes = signs[0].toByteArray();
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] fingerprintBytes = md.digest(certBytes);
// 将字节数组转换为十六进制字符串
StringBuilder fingerprintBuilder = new StringBuilder();
for (byte b : fingerprintBytes) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
fingerprintBuilder.append("0");
}
fingerprintBuilder.append(hex);
fingerprintBuilder.append(":");
}
// 移除最后的冒号
String fingerprint = fingerprintBuilder.toString();
fingerprint = fingerprint.substring(0, fingerprint.length() - 1);
return fingerprint;
}
} catch (NoSuchAlgorithmException e) {
}
return "";
}
private static Signature[] getRawSignature(Context context) {
PackageManager pkgMgr = context.getPackageManager();
try {
PackageInfo info = pkgMgr.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
if (info != null) {
return info.signatures;
}
} catch (PackageManager.NameNotFoundException e) {
return null;
}
return null;
}
还可以通过 Build.FINGERPRINT 获取,但是两种获取的结果完全不一样.
更多推荐
已为社区贡献4条内容
所有评论(0)