27

1- I'm trying to generate my key hash for integrating Android with Facebook. I understand that I have to run the following command on prompt (I am on Windows):

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

But where can I get the values of RELEASE_KEY_ALIAS and RELEASE_KEY_PATH? Please help me, I have browsed a lot and haven't find where to get them.

2- A stackoverflow answer said that another way to obtain the hash key was to download this, and run in on my android device. Buy when I import it into Eclipse I get a bunch of errors which I don't know hot to fix. The question was this one

Community
  • 1
  • 1
Adocad
  • 731
  • 1
  • 11
  • 27

3 Answers3

37

When you publish your application to the Google Play Store, you need to sign it with a Java keystore. If you haven't published yet and you don't have a keystore, you will need to configure one now. Check out the Signing Your Application documentation for more information.

RELEASE_KEY_ALIAS: Each keystore can contain multiple aliases. You could use different aliases to sign different applications, or you can sign multiple apps with the same alias. The default debug keystore for example only has one alias- androiddebugkey. If you already have a keystore and don't know what alias to use, run the command keytool -list -v -keystore YOUR_KEYSTORE_FILE to see all the available aliases.

RELEASE_KEY_PATH: This is simple the path to the keystore on your machine. It might look something like C:\Users\somezombie\myproject\release.keystore.

Once you have a keystore, you can run the command you posted to get the hash that Facebook needs. Keep in mind that Facebook might also require that you do this with your debug keystore for debug builds.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Ok, thanks, I think that solved my doubts. For now I will sign in debug mode. I put androiddebugkey as the RELEASE_KEY_ALIAS and the path of my keystore as RELEASE_KEY_PATH. However on openssl I had to put the path to where openssl.exe was. And also they asked for a password, I put "android" and I think I have my key now :D. Please correct me if I did anything wrong. – Adocad Jan 20 '15 at 16:59
2

Another way:

1- Past this code within your onCreate.

2- Run your app.

3- Check your logcat! Your hash will show in red color

public class MainActivity extends AppCompatActivity {

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

    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                getPackageName(),  //Or replace to your package name directly, instead getPackageName()  "com.your.app" 
                PackageManager.GET_SIGNATURES);

        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());

            Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {

    }
}
  • 1
    This generates debug hash is app is not signed for release , to do for the release version too install the released signed version -> apk installed eg using "adb install filename.apk" ) and use logcat to check (or use an alert or toast or copy to pasteboard...). Or you could configure android studio switching to release build variant and setting the key and the password. – Andrea Leganza Jun 06 '20 at 16:38
1

Terminal

For get Debug Hash key in Linux or macOs

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

For Windows

keytool -exportcert -alias androiddebugkey -keystore "C:\Users\USERNAME\.android\debug.keystore" | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" sha1 -binary | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" base64

Release Key For linux or Macos

 keytool -exportcert -alias androidreleasekey -keystore ~/.android/release.keystore | openssl sha1 -binary | openssl base64

For Windows

 keytool -exportcert -alias androidreleasekey -keystore "C:\Users\USERNAME\.android\release.keystore" | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" sha1 -binary | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" base64