Android 获取某应用的签名MD5

在有些项目中,有时候会需要校验apk的签名;
这时候就要先读取到该apk的签名;
这里封装一个工具类,只需知道apk的包名,就可打印对应apk的签名:
printSignatureMD5(Context mContext, String packageName)

package com.changhong.bootwizard.util;

import android.content.Context;
import android.util.Log;

import java.security.MessageDigest;

import android.content.pm.Signature;

/**
 * 作者:libeibei
 * 创建日期:20190717
 * 类说明:打印某个应用的签名MD5
 **/
public class SignTool {
    private static final String TAG = "SignTool";

    //调用示例
    //SignTool.printSignatureMD5(CHAuthService.this,"com.sccngitv.dvb");


    public static void printSignatureMD5(Context mContext, String packageName) {
        try {
            Log.e(TAG, "packageName = " + packageName + " Sign MD5 =" + getMD5MessageDigest(mContext, packageName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getMD5MessageDigest(Context mContext, String str) {
        try {
            int i = 0;
            Signature signature = mContext.getPackageManager().getPackageInfo(str, 64).signatures[0];
            MessageDigest instance = MessageDigest.getInstance("md5");
            instance.update(signature.toByteArray());
            byte[] digest = instance.digest();
            StringBuilder stringBuilder = new StringBuilder();
            int length = digest.length;
            while (i < length) {
                String toHexString = Integer.toHexString(digest[i] & 255);
                if (toHexString.length() == 1) {
                    StringBuilder stringBuilder2 = new StringBuilder();
                    stringBuilder2.append("0");
                    stringBuilder2.append(toHexString);
                    toHexString = stringBuilder2.toString();
                }
                stringBuilder.append(toHexString);
                i++;
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "null";
        }
    }
}

你可能感兴趣的:(Android 获取某应用的签名MD5)