Key hash for Android-Facebook app

342,334

Solution 1

Here are the steps-

  1. Download openssl from Google code (If you have a 64 bit machine you must download openssl-0.9.8e X64 not the latest version)

  2. Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here.

  3. detect debug.keystore file path. If u didn't find, then do a search in C:/ and use the Path in the command in next step.

  4. detect your keytool.exe path and go to that dir/ in command prompt and run this command in 1 line-

    $ keytool -exportcert -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64

    • it will ask for password, put android
    • that's all. u will get a key-hash

For more info visit here

Solution 2

[EDIT 2020]-> Now I totally recommend the answer here, way easier using android studio, faster and no need to wright any code - the one below was back in the eclipse days :) -.

You can use this code in any activity. It will log the hashkey in the logcat, which is the debug key. This is easy, and it's a relief than using SSL.

PackageInfo info;
try {
    info = getPackageManager().getPackageInfo("com.you.name", PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String something = new String(Base64.encode(md.digest(), 0));
        //String something = new String(Base64.encodeBytes(md.digest()));
        Log.e("hash key", something);
    }
} catch (NameNotFoundException e1) {
    Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
    Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
    Log.e("exception", e.toString());
}

You can delete the code after knowing the key ;)

Solution 3

I've created a small tool for Windows and Mac OS X. Just throw in the key-store file, and get the hash key.

If you want the default debug.keystore file, use the default alias and password. Else, use your own keystore file and values.

Check it out, download the Windows version or download the Mac OS X version (Dev-Host might be down sometimes... so if the link is broken, PM me and I'll fix it).

I hope that help you guys...

Dec 31, 2014 - EDIT: Changed host to AFH. Please let me know if the links are broken

Nov 21, 2013 - EDIT:

As users requested, I added a default keystore location and a DONATE button. Feel free to use it if I've helped you. :)

Screen shot Screen shot 2

Solution 4

The instructions currently in Facebook's Android Tutorial do not work well under Windows. Their example shows how to pipe the keytool output to openssl but if you try this under Windows the output is not valid for some reason. I found that I had to use intermediary files to get it to work properly. Here are the steps that worked for me:

Start by downloading openssl for Windows from Google.

C:\Users\Me>keytool -exportcert -alias my_key -keystore my.keystore -storepass PASSWORD > mycert.bin

C:\Users\Me>openssl sha1 -binary mycert.bin > sha1.bin

C:\Users\Me>openssl base64 -in sha1.bin -out base64.txt

After running these commands the valid hash is stored in the file base64.txt. Copy and paste this to your app settings on Facebook.

Solution 5

This is what is given at the official page of Facebook:

   keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

Let me break this command into fragments.

  1. Look for "keytool.exe". You can search that on the C: drive. You can find it in "java jdk" or "java jre". If you have installed multiple versions, choose any.

  2. Open a CMD prompt and go to the above directory where you found "keytool.exe".

    Clip the "exe`" and paste the above command provided on the Facebook page.

  3. You will get an error on entering this that OpenSSL is not recognized as in input output command. Solution : Download "Openssl" from OpenSSL (if you have a 64-bit machine you must download openssl-0.9.8e X64). Extract and save it anywhere... I saved it on the C: drive in the OpenSSl folder

  4. Replace the openssl in the above command in which you was getting an error of OpenSSL with "C:\OpenSSL\bin\openssl" at both the places after the pipe, "|".

  5. If prompted for a password, enter android.

And you will get your hash key. For further steps, refer again to the Facebook page.

Share:
342,334

Related videos on Youtube

ravi babu
Author by

ravi babu

Updated on May 12, 2022

Comments

  • ravi babu
    ravi babu almost 2 years

    I'm working on an Android app, in which I want to integrate a Facebook posting feature. I downloaded the Facebook-Android SDK, and I got the readme.md (text file) in there, in which it is mentioned to generate the key hash for Android. How do I generate it?

  • Walt Armour
    Walt Armour over 12 years
    This answer almost worked for me on Win7 x64. However, the resulting encoded cert was incorrect. Bryan Bedard's answer below will produce the correct cert value. I'm guessing the piping on windows is somehow the culprit.
  • Nicola Peluchetti
    Nicola Peluchetti over 12 years
    Note for 64 bit users: this works with version openssl-0.9.8e X64 only do not use with openssl-0.9.8k X64
  • Ravikiran
    Ravikiran about 12 years
    I downloaded openssl-0.9.8e X64 for my system and extracted it.It contain nothing except one file with some data. Please help me to know more.
  • Avisek Chakraborty
    Avisek Chakraborty about 12 years
    Hi user915267, download it from this link- code.google.com/p/openssl-for-windows/downloads/… It should work.
  • Antrromet
    Antrromet about 12 years
    the command should be executed in the bin folder of java in windows systems.
  • Oliver Dixon
    Oliver Dixon over 11 years
    Works perfect, everything else was failing.
  • Palani Kumar
    Palani Kumar over 11 years
    Great Answer. Thank you very much.
  • IgorGanapolsky
    IgorGanapolsky over 11 years
    Is this for debug key or for release key?
  • Bobby
    Bobby over 11 years
    What a PITA - but Bryan is right! The command will output a hash almost no matter what, whether your path is wrong, password is wrong, or the pipes aren't working right - you'll STILL GET A HASH but it won't work. So (on Windows) I ditched Powershell and tried Cygwin - still not working. Only after copying the debug.keystore file to the working dir could I get it to run and work!!
  • Bobby
    Bobby over 11 years
    So, in summary, on Windows, either use Bryan's technique of breaking it apart, OR use cygwin with your keystore file in the working dir : keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64 If it doesn't prompt you for a password then it didn't find the keystore file properly.
  • Ron
    Ron over 11 years
    Worked perfectly for me. Great answer.
  • Vaiden
    Vaiden over 11 years
    If you wanna avoid getting hashes for wrong password cases, just check your mycert.bin prior to continuing with the openSSL. Otherwise you'd be hashing the wrong password error text :)
  • Vaiden
    Vaiden over 11 years
    Also, here are the alias and password for the Android debug keystore (auto-generated by the ADT) : developer.android.com/tools/publishing/…
  • Pallavi
    Pallavi over 11 years
    @IgorGanapolsky instead of path of myKeyStore, give the path of your release key. The above code is for debug key
  • Bassem Wissa
    Bassem Wissa about 11 years
    Guys, take care, after creating the apk, the key hash is changed! because using this code u get the debug keystore hash, but when creating apk, it's another hash, gotta capture it from log after trying ur apk on emulator , then delete code and export again without this log :) - i know it's a hassle :D but for me it was easier than keytool, good luck ;)
  • Ben Sewards
    Ben Sewards almost 11 years
    @NicolaPeluchetti I'm running Win8 x64 bit and the "k" version worked for me and gave me a key-hash when I started the command off with ".\keytool ..." instead of "keytool ..."
  • Hardik Thaker
    Hardik Thaker almost 11 years
    @WilliamKinaan : anytime ;) :P
  • AnhSirk Dasarp
    AnhSirk Dasarp over 10 years
    Opensssl always creating issues. This method is the best one. Just create a blank app , and get the key printed, Use it. Thanks man!!
  • Javal Nanda
    Javal Nanda over 10 years
    I am getting different values with different alias name, how to verify which one is correct?
  • Biraj Zalavadia
    Biraj Zalavadia over 10 years
    it it asking for password with both the alias name ?
  • Javal Nanda
    Javal Nanda over 10 years
    ya and whatever password I enter it is given me the hash key.. my app is already on play store and native fb is not working now. I will need to anyhow generate exact key hash for release build. Developers are suggesting to include code inside the onCreate in following post , but i want to make it work without app update on playstore stackoverflow.com/questions/15021790/…
  • Biraj Zalavadia
    Biraj Zalavadia over 10 years
    yes there is a problem with widows with openssl. You can achieve with code specified in that post.
  • Biraj Zalavadia
    Biraj Zalavadia over 10 years
    If you want do without update on playstore. 1) Create New android Demp app. 2) Put that piece of code in on create. 3) sign this demo app with the same keystore of you app on app store 4) Then Run this signed apk 5) And use this hash key finally
  • Morten Holmgaard
    Morten Holmgaard over 10 years
    Default path to debug.keystore: C:\Users\username\.android\debug.keystore
  • Morten Holmgaard
    Morten Holmgaard over 10 years
    Take a look at Shahar2k5 answer - it is so much easier!!
  • Shahar
    Shahar over 10 years
    @MortenHolmgaard , I will try to add it in the near future, Tx for your comment.
  • Adam Varhegyi
    Adam Varhegyi over 10 years
    Best. Method. Ever. Where is the donate button ?
  • Shahar
    Shahar over 10 years
    @AdamVarhegyi - Thanks, will add one in next version :)
  • Anearion
    Anearion over 10 years
    Hi, i'm trying to use your tool but it gives me an "Missing Keystroke file" error also if i dragged the keystore into it and the path is 100% correct.
  • Shahar
    Shahar over 10 years
    @Anearion i dont know this error but i'd love to debug it. can you please add info? Windows version, the path you are using, try running as admin maybe.
  • Anearion
    Anearion over 10 years
    @Shahar2k5 Don't know really, started again and worked like a charm. Sorry
  • LeSam
    LeSam about 10 years
    Where should I put the Openssl folder, once downloaded ? doesn't work for me
  • Bryan Bedard
    Bryan Bedard about 10 years
    It's been a long time since I did this but I'm pretty sure you can save openssl in whatever folder you want and you would just need to add an entry to your Path environment variable to point to that folder for the above command prompt commands to work.
  • Vinayak
    Vinayak about 10 years
    Works like a charm! Thanks for sharing :)
  • MajorGeek
    MajorGeek over 9 years
    Its detected as Virus.whats the reason?
  • Shahar
    Shahar over 9 years
    @MajorGeek can you please add a link to a screenshot? so i can see whats the issue. there shouldn't be any thing related to virus...
  • MajorGeek
    MajorGeek over 9 years
    Here is the link. s000.tinyupload.com/?file_id=32581246108406311297 My antivirus pops up as soon as the download is complete. The extension is facebookh2ko.exe.exe
  • Jack
    Jack over 9 years
    @ShaharBarsheshetWhen I try and download this tool It installs a download manager instead of the tool?
  • Shahar
    Shahar over 9 years
    @MajorGeek and Jack i assume you downloaded the using the AD button and not the real download,. please check that the download link contains the DevHost domain name "d-h.st" check the image for more details. please update if that solved your issues. i.imgur.com/YkHPY7k.png
  • MajorGeek
    MajorGeek over 9 years
    @Shahar Barsheshet Thank you. you are right. Downloaded from Devhost domain and no issue faced.
  • Hanish Sharma
    Hanish Sharma over 9 years
    I got the key but its showing invalid switch - "$" please help where I'm wrong
  • Nicklas Møller Jepsen
    Nicklas Møller Jepsen over 9 years
    @ShaharBarsheshet Please keep a decent tone. The fact that you need to guide other people to which button they need to click to be able to download your program should cause your answer to be deleted. I know how to download your program, I just downvote your answer, simply to help avoid other people from going to the malicious website you linked to.
  • harikrishnan
    harikrishnan over 8 years
    now am using android studio.. above tool will able to use ?
  • Shahar
    Shahar over 8 years
    @harikrishnan sure! you just need the keystore file
  • Chris Cirefice
    Chris Cirefice over 8 years
    This is by far the easiest solution. Using the keytool command I was getting the wrong key hashes (I have no idea why, decided I didn't care enough to investigate). This worked and literally took 5 minutes to get the debug and release key hashes. +1
  • Loolooii
    Loolooii over 8 years
    This code does not generate a valid hash key for me. @Jamshid's answer works for me on Mac OSX.
  • Omar Faroque Anik
    Omar Faroque Anik over 8 years
    It creates wrong release key hash for me unfortunately
  • Shahar
    Shahar over 8 years
    @MdOmarFaroqueAnik i don't think that's possible, are you sure you are using the right file and the correct password and alias?
  • Gruff McGruff
    Gruff McGruff over 8 years
    You can also put the above commands into a batch script and string them together with "&&"
  • wesley franks
    wesley franks over 8 years
    @ Shahar hey I tried this and for some god awful reason my key was empty?
  • nightfixed
    nightfixed over 8 years
    I don't understand something: why is your tool giving different SHA key, compared to using the keytool command line? Isn't it supposed to produce identical results, if applied to the same keystore file?
  • Shahar
    Shahar over 8 years
    it should, and it is. are you sure you are using the same keystore file for both?
  • Zeeshan
    Zeeshan over 8 years
    @HardikThaker I used your code it gives me exactly same keyhash that i got using terminal by this command keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64 and I am still getting key hash does not match any stored key hash
  • benleung
    benleung about 8 years
    great solution, I guess there should be no executable unsigned version? unsigned version should be unable to install on any device. Those debug version are signed with debug keystore :)
  • Harsha
    Harsha over 7 years
    am generating key hash for release app downlao dfrom store and login with facebook in my device no face book app so open dialog with facebook login page and working fine.if i installed facebook app and do the same process for login with facebook facebook login page coming in that entered my details and click login show that invalid keyhash error
  • Harsha
    Harsha over 7 years
    for debug and release purposes we generate two hash keys but am integrated in facebook developer site and posted in play store and download from store and login with facebook showing invalid hash key
  • Biraj Zalavadia
    Biraj Zalavadia over 7 years
    Simple way to resolve. You will display HashKey on screen. Just type that key on App settings facebook developer console.
  • sud007
    sud007 over 7 years
    NO it asks for password. typing android gives a 24 digit hash and typing a gives 28 digit hash. No idea why!
  • Sébastien
    Sébastien over 7 years
    Is this supposed to work to get a Release hash? (when running inside an APK signed with release keystore, of course).
  • Bassem Wissa
    Bassem Wissa over 7 years
    Yes Sebastien, you just need to install the signed apk on a device connect it to android studio and check the log cat, or u can show the hash in an edittext in the ui and copy it, anyway yes the hash this code produces works with the signed apk :)
  • Akash Bisariya
    Akash Bisariya over 7 years
    what is the password here?
  • Hiren Patel
    Hiren Patel over 7 years
    @AkashBisariya, password of keystore, that you have given while creating the keystore.
  • Naeem Ibrahim
    Naeem Ibrahim over 7 years
    Generate HashKey for debug and release mode by using this. stackoverflow.com/questions/7506392/…
  • Naeem Ibrahim
    Naeem Ibrahim over 7 years
    Generate HashKey for debug and release mode by using this. stackoverflow.com/questions/7506392/…
  • Naeem Ibrahim
    Naeem Ibrahim over 7 years
    Generate HashKey for debug and release mode by using this. stackoverflow.com/questions/7506392/…
  • CodeMonkey
    CodeMonkey about 7 years
    Enjoy that beer friend.
  • KawaiKx
    KawaiKx about 7 years
    bad links! they don't have keytool at all
  • vlatkozelka
    vlatkozelka about 7 years
    We put our alias and password in this tool. Which means this can easily be sending this stuff to this tools owner- Imagine a large database of keystores + passwords -. Of course nothing is forcing anyone to use it...
  • Shahar
    Shahar about 7 years
    @vlatkozelka, check the source my friend bitbucket.org/PeleBit/keyhash_facebook_windows/src
  • Daksh Agrawal
    Daksh Agrawal almost 7 years
    It is saying Access is denied.
  • Erich García
    Erich García over 6 years
    Yes, great tool! @Shahar
  • ban-geoengineering
    ban-geoengineering about 6 years
    @HanishSharma Try leaving out the $.
  • ban-geoengineering
    ban-geoengineering about 6 years
    The default password for debug.keystore is android.
  • Tabish
    Tabish about 6 years
    using keytool is such a mess this is really simple
  • Anand Phadke
    Anand Phadke over 5 years
    Where I can find the key after successfully generated?
  • Yohan Malshika
    Yohan Malshika over 5 years
    Good answer and this help me lot.
  • Luca C.
    Luca C. over 4 years
    this is the simplest and most effective way, just take it from the logs without even openssl!
  • Khang Dinh Hoang
    Khang Dinh Hoang over 4 years
    this is the only worked solution for me!. I don't see the log like you, but I see this KeyHash: XWwXXXXX/5xxxxxxxxxxx= in the log and helped me out!
  • Shashank Setty
    Shashank Setty about 4 years
    THANK YOU SO MUCH I CAN'T BEGIN TO EXPRESS HOW GREATFUL I AM
  • Green Y.
    Green Y. about 4 years
    Thanks. I tried to get key hash by using OpenSSL. It took me for 1 min and it was easy.
  • Harrison
    Harrison about 4 years
    Thanks a lot! This was the solution as Google is signing my app now (I just use the upload certificate/keystore)
  • icyNerd
    icyNerd about 3 years
    I got the development key hash but how do i get the release key hash?
  • icyNerd
    icyNerd about 3 years
    You can find the release hash key by keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64 in Linux, Windows and MacOS
  • Biggg Jimmm
    Biggg Jimmm about 3 years
    Once your app is in play store, you need to generate 2 keyhashes: One for the SHA1 you used to generate the APK OR AAB The second to for the SHA1 generated by google.
  • Eli Nathan
    Eli Nathan about 2 years
    The link posted to fbKeyHash looks very sus...