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

Android获取应用自身相关签名信息

时间:2015-08-27 13:26:18      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:android   应用   签名   

Signature.java

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

import java.io.ByteArrayInputStream;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Signature {
    public Map<String, String> getSignatureInfo(Context context) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
            android.content.pm.Signature[] signs = packageInfo.signatures;
            android.content.pm.Signature sign = signs[0];
            byte[] signature = sign.toByteArray();

            X509Certificate cert = parseSignature(signature);

            map.put("signName", cert.getSigAlgName());

            map.put("pubKey", cert.getPublicKey().toString());

            map.put("serialNumber", cert.getSerialNumber().toString());

            map.put("sigAlgOID", cert.getSigAlgOID());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            map.put("startTime", sdf.format(cert.getNotBefore()));
            map.put("endTime", sdf.format(cert.getNotAfter()));

            map.put("subjectDN", cert.getSubjectDN().toString());

            map.put("MD5", getMessageDigest("MD5", signature));

            map.put("SHA1", getMessageDigest("SHA1", signature));

            map.put("SHA256", getMessageDigest("SHA256", signature));

            Set<Map.Entry<String, String>> entrySet = map.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                Log.i("",String.format("%s=%s", entry.getKey(), entry.getValue()));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    private String getMessageDigest(String instance, byte[] signature) {
        String sinfo = null;
        try {
            MessageDigest md = MessageDigest.getInstance(instance);

            md.update(signature);

            byte[] digest = md.digest();

            sinfo = toHexString(digest);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sinfo;
    }

    public X509Certificate parseSignature(byte[] signature) {
        X509Certificate cert = null;
        try {
            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature));
        } catch (CertificateException e) {
            e.printStackTrace();
        }
        return cert;
    }

    private void byte2hex(byte b, StringBuffer buf) {

        char[] hexChars = {‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘,

                ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘};

        int high = ((b & 0xf0) >> 4);

        int low = (b & 0x0f);

        buf.append(hexChars[high]);

        buf.append(hexChars[low]);

    }


    /**
     * Converts a byte array to hex string
     */
    private String toHexString(byte[] block) {

        StringBuffer buf = new StringBuffer();


        int len = block.length;


        for (int i = 0; i < len; i++) {

            byte2hex(block[i], buf);

            if (i < len - 1) {

                buf.append(":");

            }

        }

        return buf.toString();

    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android获取应用自身相关签名信息

标签:android   应用   签名   

原文地址:http://blog.csdn.net/vichild/article/details/48025577

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