Android在代码中如何获取应用签名及sha1值

获得App签名文件的sha1值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 获得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值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 获得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值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 获得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进制转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 将获取到得编码进行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();
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :