28 Mar 2016

How to generate key hash for facebook in android

1. Using OpenSSL and command prompt
  • Download openssl from Download
  • Extract it. Create a folder- OpenSSL in C: / and copy all files here
  • Find “debug.keystore” file path. Most likely it will be inside “C:\Users\\.android” folder. In my system it is under “C:\Program Files\Java\jdk1.7.0_67\bin”
  • Open command prompt (Run-> cmd->start) and go to java /bin folder (cd “C:\Program Files\Java\jdk1.7.0_67\bin” command will do it for you)
Now you run the below command.

C:\\Program Files\\Java\\jdk1.6.0_30\\bin>keytool -exportcert 
 -alias androiddebugkey -keystore "C:\\Users\\.android\\debug.keystore" | "C:\\OpenSSL\\bin\\openssl" sha1 -binary |"C:\\OpenSSL\bin\\openssl" base64
Enter keystore password:android

2. Using a method call from android code
Using below code snippet, you can get the hash code.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook",
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {
       
Check your logcat output for a message.
Read More !