0
votes

I am creating Android application which use Facebook login SDK.

I'd like to generate debug key hash. On Facebook website I found this command:

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.android\debug.keystore | openssl sha1 -binary | openssl base64

I modified this command to work on my computer:

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

Next I entered password: android

this command generated this hash: QUhvjKstiP5gc7hPEzwF89mwHws=

then I pasted it in facebook developer console and facebook is still saying that key hash is invalid. I know that I can copy the key hash from warning message, and paste it in facebook developer console. But my question is why keytool is generating wrong key hash?

1
I had this before and it was because I was entering the wrong password, or I was usnig the wrong debug keystore.StuStirling
Yea, but I checked this multiple times :/RediOne1
i have the same problem, i alwyas ends up with copy it from the warning msgNoam a

1 Answers

3
votes

I also had a lot of problems getting the keytool to generate a valid hash, but i implemented the method below which i found, and was able to log out a valid hash. The exact origin of the code is a bit unclear, but this blog post is a good guess.

public static String printKeyHash(Activity context) {
    PackageInfo packageInfo;
    String key = null;
    try {
        //getting application package name, as defined in manifest
        String packageName = context.getApplicationContext().getPackageName();

        //Retriving package info
        packageInfo = context.getPackageManager().getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES);

        Log.e("Package Name=", context.getApplicationContext().getPackageName());

        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            key = new String(Base64.encode(md.digest(), 0));

            // String key = new String(Base64.encodeBytes(md.digest()));
            Log.e("Key Hash=", key);
        }
    } catch (PackageManager.NameNotFoundException e1) {
        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());
    }

    return key;
}