122

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?

Janusz
  • 176,216
  • 111
  • 293
  • 365
Scott
  • 2,719
  • 4
  • 21
  • 23

21 Answers21

284

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.

Andro Selva
  • 51,960
  • 51
  • 189
  • 237
Vijay Arora
  • 3,285
  • 2
  • 12
  • 12
  • 4
    nice answer +1 for you i have followed this and get the desired result :) – BBdev Sep 17 '12 at 07:58
  • 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 Feb 10 '13 at 19:11
  • This worked for me after 2 days of confusion to what was going on. Brilliant work! – rennoDeniro Apr 02 '13 at 01:57
  • @ 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 May 23 '13 at 05:12
  • 4
    @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. – Emran Hamza May 23 '13 at 05:22
  • 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. – Naveen Kumar Sep 26 '13 at 08:22
  • 1
    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 – Diljeet Mar 23 '15 at 18:25
  • @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 – iOSAndroidWindowsMobileAppsDev Jun 23 '16 at 10:37
  • Best ans on Stack – Farrukh Faizy Nov 22 '16 at 15:34
  • Generate HashKey for debug and release mode by using this. http://stackoverflow.com/questions/7506392/how-to-create-android-facebook-key-hash/41763383#41763383 – Naeem Ibrahim Jan 20 '17 at 12:13
  • Thank you so much. Can't believe Facebook doesn't explain this all – Oscar Ortiz Jul 12 '17 at 01:31
  • 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](https://developers.facebook.com/docs/facebook-login/android) – luiswill Oct 22 '17 at 22:01
  • For me, this generates a 64 character string, but the Facebook UI insists that it should be 28 characters. – me-- Nov 17 '18 at 06:22
  • 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 May 11 '19 at 06:17
  • 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. – ACAkgul Sep 05 '20 at 16:14
150

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

AnhSirk Dasarp
  • 8,886
  • 6
  • 42
  • 53
  • 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 Feb 10 '13 at 19:11
  • @DuyguK - probably means that your key-store already exists. Try deleting that and redo. Or a wrong path you may have specified – AnhSirk Dasarp Feb 11 '13 at 12:54
  • God this took me hours. Thank you so much! – Yenthe Nov 30 '13 at 18:25
  • 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!:) – marson Jul 31 '14 at 16:12
  • will the simpler method work without emulator but on the real android device? –  Jun 18 '15 at 09:14
  • Generate HashKey for debug and release mode by using this. http://stackoverflow.com/questions/7506392/how-to-create-android-facebook-key-hash/41763383#41763383 – Naeem Ibrahim Jan 20 '17 at 12:13
  • 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](https://developers.facebook.com/docs/facebook-login/android) – luiswill Oct 22 '17 at 22:00
  • `GET_SIGNATURES` deprecated now. – Hiren Dabhi Mar 07 '19 at 12:46
  • @AnhSirkDasarp is it possible to decrypt the key hash on server and verify the authenticity of the request? – Calvin Mar 28 '19 at 10:03
33

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.

Community
  • 1
  • 1
Vicky Salunkhe
  • 4,955
  • 2
  • 30
  • 42
20

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.

allthenutsandbolts
  • 1,484
  • 1
  • 13
  • 34
  • didn't work. deleted the debug.keystore file, made debug build, file never came back. noting else in that folder updated. – CthulhuJon Mar 11 '15 at 01:38
  • 3
    I really don't know how this is the accepted answer? – Enoobong Oct 10 '16 at 00:14
  • Generate HashKey for debug and release mode by using this. http://stackoverflow.com/questions/7506392/how-to-create-android-facebook-key-hash/41763383#41763383 – Naeem Ibrahim Jan 20 '17 at 12:12
14

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

tigerden
  • 718
  • 1
  • 10
  • 27
  • Generate HashKey for debug and release mode by using this. http://stackoverflow.com/questions/7506392/how-to-create-android-facebook-key-hash/41763383#41763383 – Naeem Ibrahim Jan 20 '17 at 12:13
12

I. Create key hash debug for facebook

Add code to print out the key hash for facebook

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

    } catch (NoSuchAlgorithmException e) {

    }

II. Create key hash release for facebook

  1. Download openssl-0.9.8e_X64

  2. Make a openssl folder in C drive

  3. Extract Zip files into openssl folder

  4. Start -> Run: cmd (press enter)

  5. (press) cd C:\Program Files\Java\jdk1.6.0_45\bin. Note: C:\Program Files\Java\jdk1.6.0_45\bin: is path to jdk folder in your computer

  6. (press) keytool -exportcert -alias gci -keystore D:\folder\keystorerelease | C:\openssl\bin\openssl sha1 -binary | C:\openssl\bin\openssl base64. Note: D:\folder\keystorerelease: is path to your keystorerelease

  7. Enter keystore password: This is password when your register keystorerelease.

    Then you will have a key hash: jDehABCDIQEDWAYz5Ow4sjsxLSw=

  8. Login facebook. Access to Manage Apps. Paste key hash to your app on developers.facebook.com

Community
  • 1
  • 1
Anh Duy
  • 1,004
  • 14
  • 21
  • Generate HashKey for debug and release mode by using this. http://stackoverflow.com/questions/7506392/how-to-create-android-facebook-key-hash/41763383#41763383 – Naeem Ibrahim Jan 20 '17 at 12:13
9

SIMPLEST SOLUTION OUT THERE FOR THIS PROBLEM:

I have had this Problem for two months now. My key hashes have been pyling up to 9. Today i finally found the simple solution:

STEP 1:

Install the facebook sdk you downloaded from the facebook developer page on your phone. Don´t install the normal facebook app. Make sure you can log into facebook. Then log out.

STEP 2:

Export your app with your final release key as an apk, like you would when uploading it to the playstore.

STEP 3:

Put the Apk file on your phone via usb cable or usb stick.

STEP 4:

Install your app, using a file manager: Example

STEP 5:

Launch your app and try to log in with facebook. A dialog will open and tell you: "the key YOURHASHKEY has not been found in the facebook developer console"

STEP 6:

Write down the key.

STEP 7:

Put it into your facebook developer console and save. Now you are done. Anyone that downloads your app, published with earlier used keystore can log into facebook.

Enjoy

RevanthKrishnaKumar V.
  • 1,779
  • 1
  • 19
  • 33
Sakramento
  • 299
  • 4
  • 14
7

If you are releasing, use the keystore you used to export your app with and not the debug.keystore.

trgraglia
  • 5,083
  • 5
  • 42
  • 74
  • 1
    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.? – KunalK Feb 05 '13 at 06:17
6

At last :)

Here my story :

  1. Add this code to your main activity, after you set layout.

    try { 
      PackageInfo info = getPackageManager().getPackageInfo("PROJECTNAME", 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);
          //textInstructionsOrLink = (TextView)findViewById(R.id.textstring);
          //textInstructionsOrLink.setText(sign);
          Toast.makeText(getApplicationContext(),sign, Toast.LENGTH_LONG).show();
      }
    } catch (NameNotFoundException e) {
        Log.d("nope","nope");
    } catch (NoSuchAlgorithmException e) {
    }
    
  2. Change PROJECTNAME to your package name!

  3. Sign your app (Android Tools->Export Signed Application)
  4. In your main activity where you paste code from 2 option, in your layout create TextView with id textstring
  5. uncomment two lines, that your sign code would be set to TextView 6 Wuolia, you have your HASH , install app on your phone!!! and check your hash Key!
  6. Now when it is visible , go to facebook app you created and add it to [Key Hashes]
  7. Note that your package name should be same as on facebook [Package Name] under [Key Hashes]
  8. Have a nice day :)
Satan Pandeya
  • 3,460
  • 4
  • 22
  • 47
Vasilij
  • 61
  • 1
  • 4
6

The password of the debug certificate is android and not Android

Tarun
  • 13,357
  • 8
  • 39
  • 57
Fernando Gallego
  • 3,974
  • 27
  • 50
5

One line solution to generate for facebook

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
Anudeep Samaiya
  • 1,700
  • 1
  • 27
  • 31
4

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

1) Download Openssl

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.7.0_40\bin).

6) Copy the following code and hit enter

keytool -exportcert -alias abcd-keystore D:\Projects\MyAppFolder\keystore.txt | C:\openssl\bin\openssl sha1 - binary | C:\openssl\bin\openssl base64 ex - keytool -exportcert -alias (your sing apk alias name enter here like my sign apk alian name is abcd )-keystore "signed apk generated keystore apth enter here" | "openssl bin folder path enter here" sha1 - binary | "openssl bin folder path enter here" base64

7) Now you need to enter password, Password = (enter your sign keystore password here)

8) you got keystore which are used for release app key hash

RevanthKrishnaKumar V.
  • 1,779
  • 1
  • 19
  • 33
Vijay Rajput
  • 1,061
  • 1
  • 13
  • 17
3

Even though this thread is old, yet I would like to share my experience (recently started working with facebook), which seems to me straight:

  1. Download openssl from the link bellow: https://code.google.com/p/openssl-for-windows/downloads/list
  2. Unzip it to a local drive (e.g., C:\openssl)
  3. To get the Development key for facebook integration, use the following command from the command line in windows:

    keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.android\debug.keystore | "C:\openssl\bin\openssl.exe" sha1 -binary | "C:\openssl\bin\openssl.exe" base64

NOTE!: please replace the path for openssl.exe (in this example it is "C:\openssl\bin\openssl.exe") with your own installation path.

  1. It will prompt for password, e.g.,

Enter keystore password: android

Type android as password as shown above.

Thats it! You will be given a 28 character long key. Cheers!

Use the same procedure to get the Release key. Just replace the command with the following and use your release key alias.

keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | "PATH FOR openssl.exe" sha1 -binary | openssl base64

Mahbubul Syeed
  • 121
  • 1
  • 4
3

Generate Debug hash key

  public String hashkey(Context context) {
        String keyhash = "";
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 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));
                keyhash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            }
        } catch (PackageManager.NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }
        return keyhash;
    }

Generate Release hash key

 keytool -exportcert -alias specialbridge -keystore /home/shilpi/newproject/specialBridge/SpecialBridgeAndroid/keystore/specialbridge.jks | openssl sha1 -binary | openssl base64
2

Great blog post on the subject

Extracting the Key Hash from .p12 key

  1. Open Terminal or Command line and navigate to where your .p12 key is.
  2. Type in: “keytool -v -list -keystore mycert.p12 -storetype pkcs12″ where mycert.p12 is the filename of your .p12 key.
  3. Enter keystore password (the one you used when exported .p12 key). 4 . Copy sha1 fingerprint signature bytes text.
  4. The bytes at sha1 fingerprint signature are needed to write the “sha1.bin” file. You can use a hexadecimal editor to paste the bytes you copied. Afterwards, save the file as “sha1.bin”.
  5. Open terminal again and type in: “openssl base64 -in sha1.bin -out base64.txt”.
  6. The resulting “base64.txt” will contain the Key Hash that is needed for Facebook.

Great and simple hexadecimal editor for mac: HexFiend

OpenSSL should be preinstalled on mac, and here is the link for Windows version.

Link

RevanthKrishnaKumar V.
  • 1,779
  • 1
  • 19
  • 33
Thyselius
  • 698
  • 1
  • 8
  • 13
  • This is great info for Adobe AIR apps developers (Flash or Apache Flex). And the "Class Name" for Facebook SSO should be `AppEntry` – Alexander Farber May 11 '14 at 16:47
2

Try passing the password for the key and store as part of the command

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -keypass android -storepass android \
| openssl sha1 -binary \
| openssl base64
Sam Stern
  • 22,713
  • 12
  • 83
  • 117
1

I was able to do perform the requested task with some of the solutions here, but thought to myself, boy that is stupid... why not to write a small Java code that does this and pack it into a Jar, so I did...

A link to download the Jar

Works on Windows 8... didn't try any other OS.

RevanthKrishnaKumar V.
  • 1,779
  • 1
  • 19
  • 33
TacB0sS
  • 9,456
  • 11
  • 66
  • 111
  • 1
    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??? – TacB0sS Oct 29 '13 at 08:30
1

Hi everyone its my story how i get signed has key for facebook

first of all you just have copy this 2 methods in your first class

    private void getAppKeyHash() {
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                getPackageName(), 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));
            System.out.println("HASH  " + something);
            showSignedHashKey(something);

        }
    } catch (NameNotFoundException e1) {
        // TODO Auto-generated catch block
        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());
    }
}
public void showSignedHashKey(String hashKey) {

    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setTitle("Note Signed Hash Key");
    adb.setMessage(hashKey);
    adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    adb.show();
}

**Call funcation getAppKeyHash() from your oncreate methode if you want signed hash then make signed build install signed build and run you will get hash key in dialog then just note it and update it on facebook dev account and comment that function and make another signed APK **

1

If your password=android is wrong then Put your pc password on that it works for me.

And for generate keyHash try this link Here

Arpit Patel
  • 8,321
  • 4
  • 56
  • 70
0

The only thing working for me is using the password android. Why is that not mentioned in any guides out there?

Remi Sture
  • 8,859
  • 4
  • 18
  • 33
0

use this in kotlin for print key hash in log

try {
        val info = context.getPackageManager().getPackageInfo(context.packageName,
                PackageManager.GET_SIGNATURES);
        for (signature in info.signatures) {
            val md = MessageDigest.getInstance("SHA")
            md.update(signature.toByteArray())
            Log.d("Key hash ", android.util.Base64.encodeToString(md.digest(), android.util.Base64.DEFAULT))
        }
    }catch (e:Exception){

    }
Prashant Jajal
  • 2,856
  • 2
  • 20
  • 34