Home » Android » How to generate Keyhash (Facebook) using the Keytool in android for W7 32bit

How to generate Keyhash (Facebook) using the Keytool in android for W7 32bit

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

  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.

Answer:

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

}

Answer:

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>

Answer:

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.