40

I am using the following command to generate key hash for Facebook app console for Android

.\keytool.exe -exportcert -alias app_android -keystore release.keystore | openssl sha1 -binary | openssl base64

As told at Facebook developers SDK help

As per the help page and also the developers console, the key hash should be 28 characters long, however the keytool is generating 32 characters long key.

Java version : jdk1.8.0_31 OS : Windows 7

Generating for android.

EDIT

As per suggestion from @Shreyash-mashru, I used the following code to get the keyhash

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "my.package.name",
                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 e) {
        Log.e("KeyHash:", "++++++++++++++++++++++++++++++++++++++" + e.toString());

    } catch (NoSuchAlgorithmException e) {
        Log.e("KeyHash:", "++++++++++++++++++++++++++++++++++++++" + e.toString());
    }

However if someone can still help me out understand why the command line tool is generating 32 char long key hash instead of 28...

Jalpesh
  • 1,104
  • 1
  • 13
  • 25
  • 1
    Suggestion. Its easy to use that method which you can find from above link which you have posted and you can just get hash it into your logs. and Copy and paste from it to your facebook app.[link of that page](https://developers.facebook.com/docs/android/getting-started#troubleshooting) – shreyash mashru Oct 26 '15 at 07:25
  • Thanks @shreyashmashru will try that. But any idea why the keytool generates 32 chars instead of 28? – Jalpesh Oct 26 '15 at 09:53
  • Thank you for this question. – Bugs Happen Apr 04 '19 at 13:37

7 Answers7

108

Came across this problem too, for me I was using windows powershell and it kept generating a 32 character key. When I switch to plain old cmd it worked as expected.

Paul Cooper
  • 1,096
  • 1
  • 8
  • 3
  • Thats interesting... I was using powershell as well. Will check this out definitely when someday I am back to windows. – Jalpesh Jan 16 '17 at 18:44
  • 1
    same with me, though it was not generating 32 char long but it was showing invalid by Facebook (gving error : it should be = at the end like that), used cmd and worked perfectly – Luckyy Feb 24 '17 at 09:11
  • Well this solutions worked perfectly for my particular problem. – Jalpesh Feb 27 '17 at 07:53
  • Same here. I wonder if that is an openSSL issue or a power shell thing. – ronenmiller Sep 30 '17 at 12:31
  • It solves the issue. For those who is wondering which openssl zip/rar is suitable, I can confirm that openssl-0.9.8e_X64.zip at https://code.google.com/archive/p/openssl-for-windows/downloads is working as expected for 64-bit clients. – hanilozmen Dec 07 '20 at 09:05
  • you are my hero. Note: I did it with OpenSSL 1.1.1j 16 Feb 2021 – Floris Devreese Mar 06 '21 at 12:24
  • Man you are my personal Hero! – Hamed Zakery Miab Sep 09 '21 at 17:51
  • This is quite interested, found myself in a similar situation, but as for me I had to use cmder, some open-source command-line tool for windows – Abdulbasit Dec 30 '21 at 12:26
  • Ohh, thank you man. I was using power shell. when swich to cmd It works like charm. – Fasikaw Jan 14 '23 at 08:13
5

The generated hash is 32 characters because there is a carriage return and a newline added to the end. To fix this you can either:

Delete the last 5 characters of the hash, and add an "=" to the end. For example: "1234567890abcdefghijklmnopqrstuv" (32 chars) --> "1234567890abcdefghijklmnopq=" (28 chars)

Or:

pop open a javascript console, and use:

btoa(atob("your hash string").slice(0, -2))

Where "your hash string" is your 32 character hash.

CRice
  • 29,968
  • 4
  • 57
  • 70
1

TRY THE COMMAND IN COMMAND PROMPT !

it will ask you for password then you will get the hash key

Trying the same command on powershell doesn't provide correct hash key.

Vicky Salunkhe
  • 9,869
  • 6
  • 42
  • 59
1

try normal cmd prompt or use git bash command prompt it will generate 28 characters long string. do not use Windows PowerShell cause it generates 32 characters long string.

0

I was having the same problem. It had something to do with using my existing version of openSSL (64 bit). I downloaded the 32 bit version from here and installed it to c:\openSSL. The command then points to this version of SSL and I got my 28 character hash.

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

cbyte
  • 681
  • 7
  • 12
0

if you are using windows then open cmd and enter the below command

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

Running this in any

Lokesh
  • 209
  • 4
  • 2
-4
.\keytool.exe -exportcert -alias app_android -keystore release.keystore | openssl sha1 -binary | openssl base64

this is working fine for me. try it again.

Shivanshu Verma
  • 609
  • 5
  • 19