这就是一个工具类,直接上代码了:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

import java.security.MessageDigest;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Log;

/**
* 签名工具类
*
*
*/
public class SignUtil {
/**
* 获取对应包名的应用的签名串
*
* @param context
* @param paramString
* 应用程序包名
* @return
*/
public static String getSign(Context context, String paramString) {
if (context == null) {
throw new IllegalAccessError("Context is null!!!");
}
Signature[] arrayOfSignature = getRawSignature(context, paramString);
if ((arrayOfSignature == null) || (arrayOfSignature.length == 0)) {
Log.d("SIGN", "signs is null");
return null;
}
int i = arrayOfSignature.length;
if (i > 0) {
return getMessageDigest(arrayOfSignature[0].toByteArray());
}
return null;
}

private static Signature[] getRawSignature(Context paramContext,
String paramString) {
if ((paramString == null) || (paramString.length() == 0)) {
Log.d("SIGN", "getSignature, packageName is null");
return null;
}
PackageManager localPackageManager = paramContext.getPackageManager();
PackageInfo localPackageInfo;
try {
localPackageInfo = localPackageManager.getPackageInfo(paramString,
64);
if (localPackageInfo == null) {
Log.d("SIGN", "info is null, packageName = " + paramString);
return null;
}
} catch (PackageManager.NameNotFoundException localNameNotFoundException) {
Log.d("SIGN", "NameNotFoundException");
return null;
}
return localPackageInfo.signatures;
}

private static final String getMessageDigest(byte[] paramArrayOfByte) {
char[] arrayOfChar1 = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98,
99, 100, 101, 102 };
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
int i = arrayOfByte.length;
char[] arrayOfChar2 = new char[i * 2];
int j = 0;
int k = 0;
while (true) {
if (j >= i)
return new String(arrayOfChar2);
int m = arrayOfByte[j];
int n = k + 1;
arrayOfChar2[k] = arrayOfChar1[(0xF & m >>> 4)];
k = n + 1;
arrayOfChar2[n] = arrayOfChar1[(m & 0xF)];
j++;
}
} catch (Exception localException) {
}
return null;
}
}