[android] Facebook Android Generate Key Hash

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?

This question is related to android facebook android-keystore

The answer is


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


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

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


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


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.


The password of the debug certificate is android and not Android


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


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.


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


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


One line solution to generate for facebook

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

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.


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){

    }

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


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


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


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 **


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

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


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 :)

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to facebook

I am receiving warning in Facebook Application using PHP SDK React-Native: Application has not been registered error Can't Load URL: The domain of this URL isn't included in the app's domains Facebook OAuth "The domain of this URL isn't included in the app's domain" Facebook login message: "URL Blocked: This redirect failed because the redirect URI is not whitelisted in the app’s Client OAuth Settings." Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView` Open Facebook Page in Facebook App (if installed) on Android App not setup: This app is still in development mode IOS - How to segue programmatically using swift Get ALL User Friends Using Facebook Graph API - Android

Examples related to android-keystore

I have never set any passwords to my keystore and alias, so how are they created? SHA-1 fingerprint of keystore certificate How do I find out which keystore was used to sign an app? How do I verify that an Android apk is signed with a release certificate? Facebook Android Generate Key Hash I can’t find the Android keytool