将APK发布到应用商店或接入第三方的SDK时,有时需要提供APK的签名摘要信息,可以通过摘要算法MD5或SHA-1来获取签名的摘要,除了获取自己的APK签名,也可以获取手机上其他已安装的APK签名,只需要传入其他APK的包名即可。
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
private static final char [] HEX_CHAR = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' }; /** 获取签名的MD5摘要 */ public String[] signatureDigest() { pkgInfo = mContext.getPackageManager().getPackageInfo( mContext.getPackageName(), PackageManager.GET_SIGNATURES); int length = pkgInfo.signatures.length; String[] digests = new String[length]; for ( int i = 0 ; i < length; ++i) { Signature sign = mPkgInfo.signatures[i]; try { MessageDigest md5 = MessageDigest.getInstance( "MD5" ); byte [] digest = md5.digest(sign.toByteArray()); // get digest with md5 algorithm digests[i] = toHexString(digest); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); digests[i] = null ; } } return digests; } /** 将字节数组转化为对应的十六进制字符串 */ private String toHexString( byte [] rawByteArray) { char [] chars = new char [rawByteArray.length * 2 ]; for ( int i = 0 ; i < rawByteArray.length; ++i) { byte b = rawByteArray[i]; chars[i* 2 ] = HEX_CHAR[(b >>> 4 & 0x0F )]; chars[i* 2 + 1 ] = HEX_CHAR[(b & 0x0F )]; } return new String(chars); } |
以上就是本文的全部内容,希望对大家的学习有所帮助。