app获取自己的签名

Posted 1156740846

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了app获取自己的签名相关的知识,希望对你有一定的参考价值。

package com.tencent.wework.api.lcx;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

public class LcxSignature {

    private Activity context;

    public LcxSignature(Activity context1) {
        context = context1;
    }

    /**
     * 遍历包名
     */
    public void getSign() {
        String paramString = context.getPackageName();
        
        showToast("包名 = " + paramString);

        PackageManager pm = context.getPackageManager();
        List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
        Iterator<PackageInfo> iter = apps.iterator();
        while (iter.hasNext()) {
            PackageInfo packageinfo = iter.next();
            String packageName = packageinfo.packageName;

            if (packageName.equals(paramString)) {
                showToast("签名 = " + MD5Encode(packageinfo.signatures[0].toByteArray()));
            }
        }
    }

    /**
     * 根据包名获取签名
     */
    @SuppressLint("NewApi")
    public void getRawSignature() {

        // String paramString = "com.lc.iotstandard.ht";
        String paramString = context.getPackageName();

        PackageManager localPackageManager = context.getPackageManager();
        PackageInfo localPackageInfo;
        try {
            localPackageInfo = localPackageManager.getPackageInfo(paramString,
                    PackageManager.GET_SIGNATURES);
            if (localPackageInfo == null) {
                showToast("信息为 null, 包名 = " + paramString);
                return;
            }
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            showToast("包名没有找到...");
            return;
        }

//        String result = getSignature(paramString);
        String result = MD5Encode(localPackageInfo.signatures[0].toByteArray());
        showToast(result);

        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setPrimaryClip(ClipData.newPlainText(null, result));
    }

    /**
     * 根据包名获取签名
     * pkg: 包名
     */
    public String getSignature(String pkg) {
        try {
            PackageInfo var1;
            var1 = context.getPackageManager().getPackageInfo(pkg, PackageManager.GET_SIGNATURES);
            return MD5Encode(var1.signatures[0].toByteArray());
        } catch (Throwable var2) {
        }
        return "";
    }

    private static String MD5Encode(byte[] toencode) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.reset();
            md5.update(toencode);
            return HexEncode(md5.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    @SuppressLint("DefaultLocale")
    private static String HexEncode(byte[] toencode) {
        StringBuilder sb = new StringBuilder(toencode.length * 2);
        for (byte b : toencode) {
            sb.append(Integer.toHexString((b & 0xf0) >>> 4));
            sb.append(Integer.toHexString(b & 0x0f));
        }
        return sb.toString().toUpperCase();
    }

    public void showToast(String info) {
        Toast toast = Toast.makeText(context, info, Toast.LENGTH_LONG);
        toast.show();
    }
}

 

以上是关于app获取自己的签名的主要内容,如果未能解决你的问题,请参考以下文章

--hook,某电商(bao)app签名实现数据采集,欢迎交流

使用androidx获取片段内的actionBar

EasyClick SHA1签名获取

iPad App 的临时分发的代码签名问题

AppCan移动开发技巧:3步走,获取移动APP签名信息

Mac Electron App 签名后打开闪退