码迷,mamicode.com
首页 > 移动开发 > 详细

app获取自己的签名

时间:2020-06-04 10:32:20      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:exe   sig   board   android   name   packages   widget   ice   遍历   

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获取自己的签名

标签:exe   sig   board   android   name   packages   widget   ice   遍历   

原文地址:https://www.cnblogs.com/LcxSummer/p/13041889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!