Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of

import android.content.Context;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import android.content.pm.Signature;

import java.math.BigInteger;
import java.security.MessageDigest;

public class Main {
    /**
     * Get current certificate fingerprint
     *
     * @param ctx         context of application
     * @param packageName your package name
     * @return Base64 packed SHA fingerprint of your packet certificate
     */
    public static String[] getCertificateFingerprint(Context ctx, String packageName) {
        try {
            if (ctx == null || ctx.getPackageManager() == null)
                return null;
            PackageInfo info = ctx.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            assert info.signatures != null;
            String[] result = new String[info.signatures.length];
            int i = 0;
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                //                result[i++] = Base64.encodeToString(md.digest(), Base64.DEFAULT);
                result[i++] = toHex(md.digest());
            }
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    private static String toHex(byte[] bytes) {
        BigInteger bi = new BigInteger(1, bytes);
        return String.format("%0" + (bytes.length << 1) + "X", bi);
    }
}