Sai
Sai

Reputation: 2109

Verifying an APK programmatically using JarSigner

I want to use verify an APK from Java code using JarSigner. I am writing an app that programatically installs APKs (much like Google Play) and before installing it checks for the following:

This is much like running jarsigner with -verify option on command line

I looked up Java documentation, I think I could use JarSigner.verifyJar() API for this.But this is not public method. Has anyone tried to verify APK from Java code? Will appreciate any pointers.

Upvotes: 1

Views: 4484

Answers (1)

Buddy
Buddy

Reputation: 11038

You can get your signature like this:

PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
    context.getPackageName(), PackageManager.GET_SIGNATURES);

List<String> list = new ArrayList<>();

for (Signature signature : packageInfo.signatures) {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(signature.toByteArray());
    final String currentSignature = Base64.encodeToString(md.digest(), Base64.DEFAULT);
    list.add(currentSignature);
}

Check out this article for full details.

Upvotes: 2

Related Questions