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

7

33

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?

Marchetti answered 2/7, 2013 at 11:2 Comment(1)
try to check this answerBoiney
S
38

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

Sensate answered 2/7, 2013 at 11:26 Comment(3)
Please notice that the package name could be any thing installed on that phone, so you can get write a new app, and get other app's key hash using this method.Marchetti
Using Gradle 4.1, I didn't see the output for release the report was Variant: release Config: none, any Idea why?Trunkfish
@Trunkfish we have to set up the release config frirst. #18461274Sensate
B
49

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

Blair answered 27/3, 2015 at 15:2 Comment(5)
Thank you for offering an answer to the specific question. I needed to compare the hash of an existing APK to that of a new one to make sure they were signed with the same key. Generating the hash from the keystore file, as in the accepted answer, is not sufficient.Webbing
For OSX (no grep -P support). keytool -list -printcert -jarfile ~/Downloads/YOURAPKFILE.apk | grep "SHA1: " | cut -d " " -f 3 | xxd -r -p | openssl base64Languet
@Languet your terminal command, keytool -list -printcert -jarfile ~/Downloads/YOURAPKFILE.apk | grep "SHA1: " | cut -d " " -f 3 | xxd -r -p | openssl base64 was exactly what I was looking for. Thank youSweated
is this Hash same as the one we get from the package manager programmatically?Enhanced
can you provide the command for windows also to get the key hash from an apk?Ance
S
38

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

Sensate answered 2/7, 2013 at 11:26 Comment(3)
Please notice that the package name could be any thing installed on that phone, so you can get write a new app, and get other app's key hash using this method.Marchetti
Using Gradle 4.1, I didn't see the output for release the report was Variant: release Config: none, any Idea why?Trunkfish
@Trunkfish we have to set up the release config frirst. #18461274Sensate
T
2

Unfortunately, it is no longer possible to provide a timely response, but there is an efficient method to obtain the Signed App Key Hash. Please execute the following command in your terminal:

keytool -printcert -jarfile app.apk

Extract the SHA1 value from the output. Then, visit the below website and input the extracted hexadecimal value into the designated field. Afterward, click on the conversion button.

http://tomeko.net/online_tools/hex_to_base64.php

Please retrieve the Keyhash value from the Output (base64) field.

Toole answered 28/6, 2023 at 2:57 Comment(0)
S
1

It's too late to answer but its very quick way to get Signed app key hash.

Install apk and it can extract all apps key hash.

Download from: https://apkpure.com/key-hash-key/notimeforunch.keyhash

Sella answered 27/8, 2020 at 15:9 Comment(1)
Never late... ;)Enamelware
P
0

When I built my Facebook app. I used my Android keystore. There is a hashing function for that. Commonly used in the Google API's.(See there for instructions). If you own the app and signed it; this should be no issue otherwise..your basically screwed.There is no way.

Posting answered 2/7, 2013 at 11:6 Comment(0)
G
0

You can also use following approaches for getting Sha1 Hash in base64 (as required in case of facebook) from your apk signing keystore file:-

Mac: keytool -exportcert -alias <KEY_STORE_ALIAS> -keystore <KEY_STORE_PATH> | openssl sha1 -binary | openssl base64
 
Windows: keytool -exportcert -alias <KEY_STORE_ALIAS> -keystore <KEY_STORE_PATH> | openssl sha1 -binary | openssl base64

You would also need to have openssl for this command.

For example:

keytool.exe -list -v -keystore "%LocalAppData%\Xamarin\Mono for Android\debug.keystore" -alias androiddebugkey -storepass android -keypass android | openssl sha1 -binary | openssl base64

Where, "%LocalAppData%\Xamarin\Mono for Android\debug.keystore" should be replaced with path to your keystore file used for signing your apk (while in debugging or adhoc destribution).

Graph answered 27/1, 2021 at 9:42 Comment(0)
V
0
keytool -printcert -jarfile 123.apk

or

keytool -printcert -file CERT.RSA
Visible answered 27/10, 2022 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.