Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get key hash from signed APK?

Is there any way to get key hash from signed APK? We have a signed Android apk file, and we want to find out key hash of this APK, for Facebook SDK. Can we do that by something like jarsigner?
Any suggestions?

like image 723
Zheng Li Avatar asked Jul 02 '13 11:07

Zheng Li


People also ask

What is the benefit of signed APK?

Signing is encrypting with the private key. Because you publish the public key the app store and the users have your public key. They can decrypt your app and therefore know for sure that the app is really your own. Android and the app store does this for them.


2 Answers

On linux, I used this command to get the key hash from an apk:

 keytool -list -printcert -jarfile [path_to_your_apk] | grep -Po "(?<=SHA1:) .*" |  xxd -r -p | openssl base64 

For Mac Users (OS X) as there is no grep -P support

keytool -list -printcert -jarfile ~/Downloads/YOURAPKFILE.apk | grep "SHA1: " | cut -d " " -f 3 | xxd -r -p | openssl base64

like image 155
user521297 Avatar answered Oct 06 '22 14:10

user521297


For windows users getting the key from openssl, may be tricky some times.. I always use this to find the right signature.. Just paste this code in your onCreate() and run.

 // Add code to print out the key hash   try {   PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);   for (Signature signature : info.signatures) {   MessageDigest md = MessageDigest.getInstance("SHA");   md.update(signature.toByteArray());   Log.e("MY KEY HASH:", Base64.encodeToString(md.digest(), Base64.DEFAULT));       }   } catch (NameNotFoundException e) {    } catch (NoSuchAlgorithmException e) {    } 

Update:

Using Android studio(2.1.2):

  1. Open your project on studio and click on the gradle icon.
  2. Choose your app -> Tasks -> android -> SigningReport

This will run a gradle task that will print the debug and release certificate with md5 and sha1 keys

like image 28
amalBit Avatar answered Oct 06 '22 16:10

amalBit