Facebook Android Generate Key Hash

220,281

Solution 1

Delete your debug certificate under ~/.android/debug.keystore (on Linux and Mac OS X); the directory is something like %USERHOME%/.android on Windows.

The Eclipse plugin should then generate a new certificate when you next try to build a debug package.

Let me know if that works.

Solution 2

In order to generate key hash you need to follow some easy steps.

1) Download Openssl from: here.

2) Make a openssl folder in C drive

3) Extract Zip files into this openssl folder created in C Drive.

4) Copy the File debug.keystore from .android folder in my case (C:\Users\SYSTEM.android) and paste into JDK bin Folder in my case (C:\Program Files\Java\jdk1.6.0_05\bin)

5) Open command prompt and give the path of JDK Bin folder in my case (C:\Program Files\Java\jdk1.6.0_05\bin).

6) Copy the following code and hit enter

keytool -exportcert -alias androiddebugkey -keystore debug.keystore > c:\openssl\bin\debug.txt

7) Now you need to enter password, Password = android.

8) If you see in openssl Bin folder, you will get a file with the name of debug.txt

9) Now either you can restart command prompt or work with existing command prompt

10) get back to C drive and give the path of openssl Bin folder

11) copy the following code and paste

openssl sha1 -binary debug.txt > debug_sha.txt

12) you will get debug_sha.txt in openssl bin folder

13) Again copy following code and paste

openssl base64 -in debug_sha.txt > debug_base64.txt

14) you will get debug_base64.txt in openssl bin folder

15) open debug_base64.txt file Here is your Key hash.

Solution 3

UPDATED ANSWER (Generating through code) Simpler method :

In my experience, openssl always being troublesome, I tried the second method suggested by facebook. And it's wonderful. This is the best method to get the hash key.

Second option is to print out the key hash sent to Facebook and use that value. Make the following changes to the onCreate() method in your main activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.facebook.samples.loginhowto", 
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                }
        } catch (NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }
        ...other operations

}//end of onCreate

Replace com.facebook.samples.loginhowto with your own package name ( package name in Manifest.xml).

Official link - https://developers.facebook.com/docs/android/login-with-facebook/ ( See the bottom of the page)

OLD ANSWER (Generating Keyhash using openssl )

  1. to generate signature you need openssl installed on your pc. If you don’t have one download openssl from here
  2. In C: , Create openssl folder
  3. extract the contents of downloaded openssl zip file into openssl folder in C:drive
  4. open Command prompt
  5. move to bin of openssl i.e C:\openssl\bin in command prompt
  6. run the following command to generate your keyhash. While generating hashkey it should ask you password.

    keytool -exportcert -alias androiddebugkey -keystore "C:\Users\Anhsirk.android\debug.keystore" | openssl sha1 -binary | openssl base64

NOTE: in the above code note that , you need to give your path to user ( i.e in my case it is C:\Users\Anhsirk , you just need to change this for your user account.

Give password as android

. If it don’t ask for password your keystore path is incorrect.

If everything works fine, it should give you the hashkey below.

enter image description here

Solution 4

Simplest way to generate hash key.

Requirement: SHA1 Key

You can get SHA1 Key from your keystore file by two ways

1) Locate your keystore file, open command prompt on that location then use below mentioned command

keytool -list -v -keystore {keystore_name} -alias {alias_name}

and then enter your password then it will return md5, sha1 and sha256 key.

OR

2) By running signingReport

Refer below image.

enter image description here

after you run the file your output will be generated containing required sha1 key.

enter image description here

After you get the required SHA1 Key

Then goto

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

and paste your sha1 key

enter image description here

and finally you will get Required HashKey which you can use it to apply on facebook.

Solution 5

The right key can be obtained from the app itself by adding the following code to toast the proper key hash (in case of Facebook SDK 3.0 onwards, this works)

try {
            PackageInfo info = getPackageManager().getPackageInfo("com.package.mypackage",         PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                String sign=Base64.encodeToString(md.digest(), Base64.DEFAULT);
                Log.e("MY KEY HASH:", sign);
                Toast.makeText(getApplicationContext(),sign,         Toast.LENGTH_LONG).show();
            }
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}

Replace com.package.mypackage with your package name

Share:
220,281
Scott
Author by

Scott

Developer at TheHandsomeCoder

Updated on October 08, 2020

Comments

  • Scott
    Scott over 3 years

    Trying to create an android app with Facebook integration, I've gotten to the part in the docs where you have to generate a key hash file, it specifies to run the following code

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

    When I run this in my terminal I get an error for Keystore tampered with or password was incorrect,

    I just want to generate my Key Hash

    Can anyone point me in the right direction?

  • BBdev
    BBdev almost 12 years
    nice answer +1 for you i have followed this and get the desired result :)
  • KunalK
    KunalK over 11 years
    Hi, i've tried using my application keystore to generate the keyhash, but whenever i try to share anything from my application it gives me error that Application is misconfigured for Facebook login. but when i try keyhash generated through debug.keystore it works. can u please help me on this.?
  • DuyguK
    DuyguK over 11 years
    I am getting this: keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect.How can i find my correct password?
  • DuyguK
    DuyguK over 11 years
    I am getting this: keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect.How can i find my correct password?
  • AnhSirk Dasarp
    AnhSirk Dasarp over 11 years
    @DuyguK - probably means that your key-store already exists. Try deleting that and redo. Or a wrong path you may have specified
  • rennoDeniro
    rennoDeniro about 11 years
    This worked for me after 2 days of confusion to what was going on. Brilliant work!
  • Emran Hamza
    Emran Hamza about 11 years
    @ DuyguK: put password as android then press enter, Then you will not get this error message keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect.How can i find my correct password?
  • Emran Hamza
    Emran Hamza about 11 years
    @Vizzz: Nice way of explain for generating key hash. Thanks for this post, hope this post will save a lot of time of some one.
  • Naveen Kumar
    Naveen Kumar over 10 years
    In my case when i run this command in cmd openssl sha1 -binary debug.txt > debug_sha.txt it gives the error Access is denied.
  • TacB0sS
    TacB0sS over 10 years
    What I don't understand is, if it took me a couple of hours to learn it all and sketch this up, why none of the big companies have done this already to provide us with an easy way to get the info they ask from us???
  • Yenthe
    Yenthe over 10 years
    God this took me hours. Thank you so much!
  • Alexander Farber
    Alexander Farber about 10 years
    This is great info for Adobe AIR apps developers (Flash or Apache Flex). And the "Class Name" for Facebook SSO should be AppEntry
  • marson
    marson almost 10 years
    I want to ask, if the new method (get keyhashes by code) slows somehow the onCreate process. Because from what i know, when the signed APK is created, the keyhashes are different. Thanks for the code anyways!:)
  • CthulhuJon
    CthulhuJon over 9 years
    didn't work. deleted the debug.keystore file, made debug build, file never came back. noting else in that folder updated.
  • Diljeet
    Diljeet over 9 years
    I tried every code but never got the desired output. i tried code from facebook it also is the same just all three commands in one command, but running three commands seperately gave the right code and worked, hour of search or maybe days of search. Thanks. Before this only the code on android app was giving right result, command prompt gave invalid keys, now all is well. Thanks again
  • akshay7692
    akshay7692 about 9 years
    just a note : you need to put '=' after the key copied from log
  • Admin
    Admin about 9 years
    will the simpler method work without emulator but on the real android device?
  • iOSAndroidWindowsMobileAppsDev
    iOSAndroidWindowsMobileAppsDev about 8 years
    @VijayArora Your install instructions are clear but the second openssl results in an 'access denied' error it was asier to use this one line: keytool -exportcert -alias androiddebugkey -keystore "%USERPROFILE%\.android\debug.keystore" | C:\openssl\bin\openssl.exe sha1 -binary | C:\openssl\bin\openssl.exe base64
  • Harsha
    Harsha over 7 years
    but it showing asking password am giving signed keystore password also and then showing chines laungauge
  • Enoobong
    Enoobong over 7 years
    I really don't know how this is the accepted answer?
  • Farrukh Faizy
    Farrukh Faizy over 7 years
    Best ans on Stack
  • 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/…
  • 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/…
  • KawaiKx
    KawaiKx about 7 years
    ensure that the path is correctly set for openssl\bin and java...\bin
  • Oscar Ortiz
    Oscar Ortiz almost 7 years
    Thank you so much. Can't believe Facebook doesn't explain this all
  • luiswill
    luiswill over 6 years
    That's for the debug environnement so it means just to test not when it's published. If you want release key hash : keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64 more info on Facebook Docs
  • luiswill
    luiswill over 6 years
    That's for the debug environnement so it means just to test not when it's published. If you want release key hash : keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64 more info on Facebook Docs
  • me--
    me-- over 5 years
    For me, this generates a 64 character string, but the Facebook UI insists that it should be 28 characters.
  • Hiren Dabhi
    Hiren Dabhi over 5 years
    GET_SIGNATURES deprecated now.
  • Calvin
    Calvin about 5 years
    @AnhSirkDasarp is it possible to decrypt the key hash on server and verify the authenticity of the request?
  • hakuna1811
    hakuna1811 about 5 years
    For anyone that tried but still does not work, use storepass as extra params, like this keytool -exportcert -alias MY_APP_DEBUG -storepass 678910 -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
  • hakuna1811
    hakuna1811 about 5 years
    This answer is much simpler than the others since it only cares about the final output. Always correct. In other cases, if the signing process misconfigured, we won't know how to fix.
  • Dheeraj Jaiswal
    Dheeraj Jaiswal about 5 years
    Awesome Work like charm Thank buddy
  • ACAkgul
    ACAkgul almost 4 years
    I can't believe this method make me realized I ve been writing "upper case i" letter as "lower case L" in the hash key which facebook show me. Thank you.
  • S_i_l_e_n_t  C_o_d_e_r
    S_i_l_e_n_t C_o_d_e_r almost 3 years
    This answer is easy way to generate Hash Key. Thank You