Android在代码中如何获取应用签名及sha1值
获得App签名文件的sha1值
/**
* 获得App签名文件的sha1值
*
* @param context
* @return
*/
public static String getAppSignSha1(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(),
PackageManager.GET_SIGNATURES);
// X509证书,X.509是一种非常通用的证书格式
Signature[] signs = packageInfo.signatures;
Signature sign = signs[0];
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(sign.toByteArray()));
// sha1
MessageDigest md = MessageDigest.getInstance("SHA1");
// 获得公钥
byte[] b = md.digest(cert.getEncoded());
return bytes2HexFormatted(b);
} catch (Exception e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return "";
}
获得App签名文件的sha256值
/**
* 获得App签名文件的sha256值
*
* @param context
* @return
*/
public static String getAppSignSha256(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(),
PackageManager.GET_SIGNATURES);
// X509证书,X.509是一种非常通用的证书格式
Signature[] signs = packageInfo.signatures;
Signature sign = signs[0];
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(sign.toByteArray()));
// sha1
MessageDigest md = MessageDigest.getInstance("SHA256");
// 获得公钥
byte[] b = md.digest(cert.getEncoded());
return bytes2HexFormatted(b);
} catch (Exception e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return "";
}
获得App签名文件的md5值
/**
* 获得App签名文件的md5值
*
* @param context
* @return
*/
public static String getAppSignMd5(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
//请注意需要PackageManager.GET_SIGNATURES 这个flag
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(),
PackageManager.GET_SIGNATURES);
Signature[] signs = packageInfo.signatures;
Signature sign = signs[0];
// X509证书,X.509是一种非常通用的证书格式
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory
.generateCertificate(new ByteArrayInputStream(sign.toByteArray()));
// md5
MessageDigest md = MessageDigest.getInstance("MD5");
// 获得公钥
byte[] b = md.digest(cert.getEncoded());
//key即为应用签名
return bytes2HexFormatted(b);
} catch (PackageManager.NameNotFoundException e) {
NLog.e(TAG, Log.getStackTraceString(e));
} catch (CertificateException e) {
NLog.e(TAG, Log.getStackTraceString(e));
} catch (NoSuchAlgorithmException e) {
NLog.e(TAG, Log.getStackTraceString(e));
}
return "";
}
字节数组16进制转换
/**
* 将获取到得编码进行16进制转换
*
* @param arr
* @return
*/
private static String bytes2HexFormatted(byte[] arr) {
StringBuilder str = new StringBuilder(arr.length * 2);
for (int i = 0; i < arr.length; i++) {
String h = Integer.toHexString(arr[i]);
int l = h.length();
if (l == 1)
h = "0" + h;
if (l > 2)
h = h.substring(l - 2, l);
str.append(h.toUpperCase());
if (i < (arr.length - 1))
str.append(':');
}
return str.toString();
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/29/how-to-obtain-application-signatures-and-sha1-values-in-android-code/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
1
二维码
打赏
海报
Android在代码中如何获取应用签名及sha1值
获得App签名文件的sha1值
/**
* 获得App签名文件的sha1值
*
* @param context
* @return
*/
public static String getAppSignSha1(Context context) {
……
文章目录
关闭
共有 0 条评论