By Waypoint

2011-04-07 09:27:28 8 Comments

Is there a way to retrieve the signature of the key used to sign an APK? I signed my APK with my key from my keystore. How can I retrieve it programmatically?


@Shengfeng Li 2016-07-27 15:18:02

My situation is that I have a pre-installed apk which use a wrong key-store . So directly install will give a fail because of inconsistent signature.I need to check the signature first to make sure it can be install smoothly.

Here is my solution.

As this code says, you can get the signature from an installed apk.


Signature sig = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures[0];

Secondly: get the releaseApk hashCode to compare. In my case, I downloaded this apk from my server, and put it in the sd_card.

Signature releaseSig = context.getPackageManager().getPackageArchiveInfo("/mnt/sdcard/myReleaseApk.apk", PackageManager.GET_SIGNATURES).signatures[0];

Finally,compare the hashCode.

return sig.hashCode() == releaseSig.hashCode;

I have tried the code above, it works just fine. If the hashCode is different, you should just uninstall the old apk or if it's a system app and the device is rooted, you can just use runtime to remove it,and then install the new signature apk.

@frogoscar 2017-10-19 12:49:35

getPackageArchiveInfo, not getPackageAchiveInfo

@Yojimbo 2013-04-30 17:12:35

The package manager will give you the signing certificate for any installed package. You can then print out the details of the signing key, e.g.

final PackageManager packageManager = context.getPackageManager();
final List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES);

for (PackageInfo p : packageList) {
    final String strName = p.applicationInfo.loadLabel(packageManager).toString();
    final String strVendor = p.packageName;

    sb.append("<br>" + strName + " / " + strVendor + "<br>");

    final Signature[] arrSignatures = p.signatures;
    for (final Signature sig : arrSignatures) {
        * Get the X.509 certificate.
        final byte[] rawCert = sig.toByteArray();
        InputStream certStream = new ByteArrayInputStream(rawCert);

        try {
            CertificateFactory certFactory = CertificateFactory.getInstance("X509");
            X509Certificate x509Cert = (X509Certificate) certFactory.generateCertificate(certStream);

            sb.append("Certificate subject: " + x509Cert.getSubjectDN() + "<br>");
            sb.append("Certificate issuer: " + x509Cert.getIssuerDN() + "<br>");
            sb.append("Certificate serial number: " + x509Cert.getSerialNumber() + "<br>");
        catch (CertificateException e) {
            // e.printStackTrace();

@JY2k 2017-01-02 14:40:18

What is the command to validate the file with the corresponding serialNumber: unzip -p [path_to_apk] META-INF/CERT.RSA | keytool -printcert

@Ollie C 2011-04-07 09:57:48

You can access the APK's signing signature like this using the PackageManager class

Signature[] sigs = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
for (Signature sig : sigs)
    Trace.i("MyApp", "Signature hashcode : " + sig.hashCode());

I've used this to compare with the hashcode for my debug key, as a way to identify whether the APK is a debug APK or a release APK.

@Zennichimaro 2012-08-08 08:26:47

how do you know whether it is signed with debug or release key? I get: 08-08 08:24:36.668: I/Platform(19131): Signature hashcode : -1907392821 I don't even know how to verify whether it is my package or not... (I need to verify whether com.mypackage.myapp belongs to me or not)

@Zennichimaro 2012-08-08 08:38:42… lets me differentiate between debug or release apk, but is there a way to validate my signature against my public key to verify the apk actually comes from me? Thanks!

@Ruzard 2013-09-03 19:24:15

is there a way to get a signature of a not installed application? For example if I have an .apk file

@thejh 2014-10-29 13:24:50

this approach is not collision resistant

@CommonsWare 2015-08-20 19:18:05

For serious cases, don't use hashCode(). For example, if you want to validate that some other app that you want to communicate with was signed with the right key, use a cryptographically-secure hash (e.g., SHA256).

@Shengfeng Li 2016-07-27 14:44:15

@Ruzard Yes, you can. You need to push the not installed apk to the location where you program can get the apk such as SD_CARD, then use context.getPackageManager().getPackageAchiveInfo(NOT_INSTALL‌​_APK_PATH,PackageMan‌​ager.GET_SIGNATURES) .signatures[0] to get its hashCode. and compare them . Good luck

@eRaisedToX 2017-01-20 10:41:41

what does Trace.i() method do..? Somebody please help me understand.

@swooby 2018-02-23 00:18:27

Why is this the accepted answer? .hashCode() just returns the Java Object's has code, which is useless to know in this context.

@Hossein Shahdoost 2018-04-04 16:26:50

@CommonsWare shouldn't hashCode return the id of the object in memory?

@CommonsWare 2018-04-04 16:31:40

@HosseinShahdoost: Ummm... yes. Which even further means that you shouldn't use hashCode() here. :-)

@Hossein Shahdoost 2018-04-05 07:54:12

@CommonsWare well, I asked a question about this here… and apparently they have overwritten the method Signature.hashCode(), it returns the hashCode of the signature not the object in memory

@fhenneke 2020-02-26 14:21:57

@HosseinShahdoost Yes, they have overwritten it, but it is still not safe to use. The computed hash consists of 32 bits, which allows for trivial collisions. See for details.

@Hossein Shahdoost 2020-03-03 16:10:36

@fhenneke of course, the returned hash shouldn't be used for anything serious but the main problem was that they have decided to override such a fundamental method. I think the whole idea was wrong.

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] Is there a way to get the source code from an APK file?

31 Answered Questions

[SOLVED] How to create a release signed apk file using Gradle?

45 Answered Questions

[SOLVED] Getting Chrome to accept self-signed localhost certificate

53 Answered Questions

34 Answered Questions

[SOLVED] How to avoid reverse engineering of an APK file?

39 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

15 Answered Questions

[SOLVED] How to create a self-signed certificate with OpenSSL

24 Answered Questions

[SOLVED] How do I get an apk file from an Android device?

  • 2010-10-27 12:06:20
  • Finder
  • 422803 View
  • 483 Score
  • 24 Answer
  • Tags:   android adb apk

33 Answered Questions

[SOLVED] How do you install an APK file in the Android emulator?

Sponsored Content