I have an installed app & an APK file, and would like to verify that the installed app & the APK file are signed with the same certificate. The method I use is to compare the certificate serial number:
Get certificate serial number of installed app:
Signature sig = context.getPackageManager().getPackageInfo("com.myapp", PackageManager.GET_SIGNATURES).signatures[0];
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(sig.toByteArray()));
Log.d(TAG, "SerialNumber: " + cert.getSerialNumber().toString(16));
Get certificate serial number of APK file:
unzip -p build/android/bin/MyApp.apk META-INF/*.RSA | keytool -printcert | grep Serial
So I receive the serial number something like this: 4f277710
I want to know if this method is good enough, or are there any other better ways?