android – Find the key hash for a signed app-ThrowExceptions

Exception or error:

I have signed my app and exported it to a folder on my desktop called app in this folder is my app itself and the keystore. How do i find the key hash that i can copy into the facebook developers page. i have openssl installed but cant seem to generate the key hash ive tried many other threads on stack and none have seemed to help, thanks

James

How to solve:
  1. You should know where is your keystore file. For me is C:\Users\Selvin\Desktop\selvin.kp
  2. You should know your alias in keystore. For me is selvin
  3. You should know path to keytool. C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe
  4. You should know path to openssl. C:\OpenSSL-Win32\bin\openssl.exe
  5. You should know password to keystore. For me is ***** hehe

Then, you should call:

C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe” -exportcert -alias selvin -keystore c:\users\selvin\desktop\selvin.kp | C:\OpenSSL-Win32\bin\openssl sha1 -binary | C:\OpenSSL-Win32\bin\openssl base64

Replace my path and alias with proper ones.

Then you should see:

Enter keystore password:

Enter your password and you should get something like this: NfhDlIH7XWJzUZRjL+pZySrMX1Q=

EDITED: NfgDlIG7XWJzUZRUL+bZySrMX1Q= <- is a bad hash. Or you got so lucky that your key made the same collision as

error:keytool error: java.lang.Exception: Alias does not exist

If hash not working:

First, call

C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe” -exportcert -alias selvin -keystore c:\users\selvin\desktop\selvin.kp

Type password and read the error

If you don’t remember your alias keytool error: java.lang.Exception: Alias <selvinn> does not exist I used selvinn to show error.

For a list of all your entries/aliases:

C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe -list -keystore c:\users\selvin\desktop\selvin.kp

second edit

enter image description here

###

For those still struggling I have found that these steps when followed correctly will certainly work but they can be quite challenging to get right first time, and actually I have found that sometimes the base64 conversion of the fingerprint when dealing with some alias’ do not work (the hash gets truncated for some reason). I’ve written various batch files that pull most of these instructions already mentioned together so I don’t rule out a problem there.

However, essentially most people fall down at the openssl stage (either can’t find it, don’t know how to use it, or Windows piping doesn’t correctly chain the output from the SHA1 export to the base64 conversion input).

To get around this you can use an alternative method which is probably easier to follow and understand. Essentially what the facebook API wants is the base64 representation (encoding) of the SHA1 hash used to fingerprint your APK. To do this you can just list the keystore:

"C:\Program Files\Java\JRE6\Bin\keytool.exe" -list -v -keystore "Path-to-your-keystore" -storepass "KeystorePassword" > somefile.txt

Obviously you need to change the path to the keytool executable according to your own setup, and replace “Path-to-your-keystore” and “KeystorePassword” with your keystore path and password! The result should be the creation of a file “sometext.txt” in the current folder which you can then open in any text editor. The text file will list all of the keystore alias’ and their respective MD5 and SHA1 hashes as hex strings.

Now just find the alias used to sign your APK, copy the SHA1 hash, and use any online hex to base64 converter to convert it to the base64 encoding format that facebook requires. You can find an online converter by googling “online hex to base64 converter”. I’ve been using this one as you can just copy and paste the string right from the text file in to the box provided and it will just remove the colons that separate each hex byte.

One final point (somewhat obvious but..) only copy and paste the hex string and NOT the SHA1: prefix!

Hope this helps someone; it certainly works for me!

###

First i would like to thank Selvin!

This answer is almost identical to Selvin’s answer but it still took me 3 hours to get it to work ๐Ÿ˜› so a little more tutorial for the real newbs

How to get a hashKey from a keystore

  • first install OpenSSL from google code and put it in your C:\ folder
    download link
  • find keytool program location (default in java
    folder)
  • find keystore location (there is a debug default value)

location keytool C:\Program Files (x86)\Java\jdk1.xxx)\bin\keytool
location openssl C:\OpenSSL-Win32\bin\OpenSSL
location (debug)keystore C:\Users[usernamepc].android\debug.keystore

open cmd in windows(start->run->cmd) and navigate to the keytool location
or copy paste the following string, note that you canโ€™t use ctrl+v but right mouseclick

cd c:\program files (x86)\java\jdk1.7.0_01\bin

when in the right directory paste this string in the cmd line:

keytool.exe -exportcert -alias androiddebugkey -keystore c:\users\charx\.android\debug.keystore | C:\OpenSSL-Win32\bin\openssl sha1 -binary | C:\OpenSSL-Win32\bin\openssl base64

“C:\OpenSSL-Win32\bin\openssl” is the path of opensssl.exe replace it with your openssl.exe’s path

make sure that you change the [usernamepc] of yourto the name of your pc as you can see in my case itโ€™s charx. Also the directory for the java jdk xxxx depends on your version.

cmd should show hashkey

hash key for my debug file is

h1GdQbgB8b/liCG+acmZWkgIRHA=

###

A solution with Eclipse (for lazy people like me).

Select any project in Eclipse, Export – Android Application, enter your key pass etc. and on the last page at the bottom you will see
MD5: ….
SHA1:….

Copy the SHA1 string and paste it in any HEX-Base64 online converter (for example this one: http://tomeko.net/online_tools/hex_to_base64.php)

Congrats! You’ve got the key hash needed for Facebook Dev Console.

###

The solutions mentioned above didn’t work for me for some reason but i was able to successfully generate keyhash. I am writing the 10 easiest step to get keyhash of your signed apk [apk signed with keystore]:

  1. Copy the below code into your activity [start Activity].This code should be contained in your activity so that you can extract the proper keyhash when your signed apk’s Activity starts.

    private void getHashKey() {
    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) {
    } }
    
  2. Your AndroidManifest.xml file should have the attribute android:allowBackup="true" in its application tag.

  3. Export your signed apk and Install the app in your mobile phone and then connect your phone in debugging mode with usb debugging on.

  4. Then go to sdk\platform-tools

  5. Open command prompt and type adb devices to see whether your device is connected or not. if device not listed then try to fix that issue before continuing to next step.

  6. Then type adb logcat >"log.txt". Your cmd screen will like hanged.
    Dont Panic. Its Perfectly normal as the whole logcat is being written to log.txt.

  7. Run your app and hopefully when you think that the getHashKey()
    function is executed then press ctrl+c on command prompt to end log file writing.

  8. Now the command prompt will become responsive again. Now go into
    your sdk\platform-tools dir and you will see that a log.txt file
    has been created which contains logs.

  9. Now open it in a texteditor and search for MY_KEY_HASH:“———–Your keyhash——-“

  10. Copy this onto your FB account or whereever you need it and then make another build in which the android:allowBackup="false" and getHashKey() function is
    removed.

Hope this helped everyone ๐Ÿ™‚

###

I couldnt be bothered with all this I see what the facebook sdk is actually sending by
exporting my signed App with temp code in place to display facebook error.toString()
in Authorize which gives the hash key its looking for I then put this in my facebook App and bingo !

###

if you use your alies use this command to find keystore alies

keytool -list -v -keystore keystore.jks | findstr “Alias Creation”

Leave a Reply

Your email address will not be published. Required fields are marked *