8

I tried to generate the Keyhash for integrating the Facebook in our app, but when i generated the keyhash through cmd prompt, it can't generate.

C:\Users\DON\.android>keytool -exportcert -alias androiddebugkey -keystore ~/.an
    droid/debug.keystore | openssl sha1 -binary | openssl base64

'keytool' is not recognized as an internal or external command, operable program or batch file.

and another command I use is:

C:\Program Files\Java\jdk1.6.0_20\bin>keytool -exportcert -alias androiddebugkey
     -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

'openssl' is not recognized as an internal or external command, operable program or batch file.

What is the problem?

Rishi Gautam
  • 1,948
  • 3
  • 21
  • 31

5 Answers5

12

Using this command first download this file http://code.google.com/p/openssl-for-windows/downloads/detail?name=openssl-0.9.8k_WIN32.zip . Then extract the file and run this command:

C:\Program Files\Java\jdk1.6.0_20\bin>keytool -export -alias myAlias -keystore C:\Users\DON\.android\myKeyStore | C:\openssl\bin\openssl sha1 -binary | C:\openssl\bin\openssl enc -a -xtIm30l*********=

DON is my system name and should be replaced with your system name.

Ryan Berger
  • 9,644
  • 6
  • 44
  • 56
Rishi Gautam
  • 1,948
  • 3
  • 21
  • 31
  • In case it says: unknown option '-xtlm301*********='. It worked by replacing with base64 at the end. keytool -export -alias myAlias -keystore C:\DON\.android\myKeyStore | C:\openssl\bin\openssl sha1 -binary | C:\openssl\bin\openssl base64 @Rishi Gautam -- Thanks !! saved my day. – Biswas Khayargoli Feb 21 '17 at 17:55
9
  1. Download the openssl-for-windows package.
  2. Extract the zip.
  3. In windows, edit the path system variable that points to <openssl-extracted-folder>/bin
  4. Then run the command.
Mushtaq Jameel
  • 7,053
  • 7
  • 33
  • 52
4

First do the Facebook sdk setup then main program if you add this, you will get keyhash at console

There will be chances of 3 type keys once is debug and another is release key and after upload google changes signature ,you can provide all these 3 keys to facebook developer account ,then you can check facebook login. depending upon your app mode facebook will match the key.Use toast to see the keyhash ,if you dont know android monitor from android studio

import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
   printKeyHash();
}


private void printKeyHash() {
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                getPackageName(), 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 (PackageManager.NameNotFoundException e) {
        Log.e("jk", "Exception(NameNotFoundException) : " + e);
    } catch (NoSuchAlgorithmException e) {
        Log.e("mkm", "Exception(NoSuchAlgorithmException) : " + e);
    }
}

}

Vinayak Shedgeri
  • 2,222
  • 1
  • 21
  • 24
2

it's late answer but it will help to lazy people like me.. add this code to your Application class, there is no need to download openssl and no need to set the path.. only need is just copy my code.. and keyHash will generated in log.

import com.facebook.FacebookSdk;
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
        printKeyHash();
    }

    private void printKeyHash() {
        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    getPackageName(), 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 (PackageManager.NameNotFoundException e) {
            Log.e("jk", "Exception(NameNotFoundException) : " + e);
        } catch (NoSuchAlgorithmException e) {
            Log.e("mkm", "Exception(NoSuchAlgorithmException) : " + e);
        }
    }
}

and do not forget add MyApplication class in manifest:

<application
        android:name=".application.MyApplication"
</application>
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
0

Download openssl from https://code.google.com/archive/p/openssl-for-windows/downloads.

Extract the file and then execute in CMD, replancing the routes and the KeyName

keytool -exportcert -alias KEYNAME -keystore "C:\Users\YOUR_USER\.android\debug.keystore" 
| "C:\URL_OPENSSL_EXTRACTED\bin\openssl" sha1 -binary 
| "C:\URL_OPENSSL_EXTRACTED\bin\openssl" base64.
Tom
  • 1,516
  • 1
  • 14
  • 34