Facebook Android Generate Key Hash

AndroidFacebookAndroid Keystore

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

  1. Now you need to enter password, Password = android.

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

  3. Now either you can restart command prompt or work with existing command prompt

  4. get back to C drive and give the path of openssl Bin folder

  5. copy the following code and paste

> openssl sha1 -binary debug.txt > debug_sha.txt

  1. you will get debug_sha.txt in openssl bin folder

  2. Again copy following code and paste

> openssl base64 -in debug_sha.txt > debug_base64.txt

  1. you will get debug_base64.txt in openssl bin folder

  2. open debug_base64.txt file Here is your Key hash.

Solution 2 - 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

Solution 3 - Android

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 4 - Android

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 5 - Android

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

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

The password of the debug certificate is android and not Android

Solution 10 - Android

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

Solution 11 - Android

One line solution to generate for facebook

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

Solution 12 - Android

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

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

  2. you got keystore which are used for release app key hash

Solution 13 - Android

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

Solution 14 - Android

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

Solution 15 - Android

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

Solution 16 - Android

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

Solution 17 - Android

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.

Solution 18 - Android

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

Solution 19 - Android

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

Solution 20 - Android

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

Solution 21 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScottView Question on Stackoverflow
Solution 1 - AndroidVijay KumarView Answer on Stackoverflow
Solution 2 - AndroidAnhSirk DasarpView Answer on Stackoverflow
Solution 3 - AndroidVicky SalunkheView Answer on Stackoverflow
Solution 4 - AndroidallthenutsandboltsView Answer on Stackoverflow
Solution 5 - AndroidtigerdenView Answer on Stackoverflow
Solution 6 - AndroidAnh DuyView Answer on Stackoverflow
Solution 7 - AndroidsakramentoView Answer on Stackoverflow
Solution 8 - AndroidAnthony GragliaView Answer on Stackoverflow
Solution 9 - AndroidFernando GallegoView Answer on Stackoverflow
Solution 10 - AndroidVasilijView Answer on Stackoverflow
Solution 11 - AndroidAnudeep SamaiyaView Answer on Stackoverflow
Solution 12 - AndroidVijay RajputView Answer on Stackoverflow
Solution 13 - AndroidMahbubul SyeedView Answer on Stackoverflow
Solution 14 - AndroidAshutosh SrivastavaView Answer on Stackoverflow
Solution 15 - AndroidThyseliusView Answer on Stackoverflow
Solution 16 - AndroidSam SternView Answer on Stackoverflow
Solution 17 - AndroidTacB0sSView Answer on Stackoverflow
Solution 18 - AndroidPrashant Maheshwari AndroView Answer on Stackoverflow
Solution 19 - AndroidArpit PatelView Answer on Stackoverflow
Solution 20 - AndroidRemi StureView Answer on Stackoverflow
Solution 21 - AndroidPrashant JajalView Answer on Stackoverflow