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.

7 comments:

  1. Hi there, You've done a fantastic job. I’ll certainly digg it and personally recommend to my friends. I'm confident they'll be benefited from this site. apple support berlin

    ReplyDelete
  2. One of the knowledge stuffed blogs i've ever seen. It helps revamp existing technology and architecture, using constantly evolving strategies to drive improved market performance. reffer the given blog for more info Application Modernization Services or
    reach +18882075969 for more information

    ReplyDelete
  3. Really helpfull and easiest way to understand the concepts. Thanks for sharing please continue this wonderfull Work. If you want to know Anything about mendix Dont hesitate to Visit Mendix Certified developers | Mendix License or Reach out me @ +18882075969

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I just read your blog and I must say, it's truly remarkable! Your writing style is engaging, and your insights are thought-provoking. Keep up the great work!

    As Maven Technology we named as Best Website Development Company in Delhi : Here you unleash digital potential with cutting-edge solutions across all digital channels. From responsive design to user-friendly interfaces, we excel at creating compelling websites that drive results.

    Let's transform your digital presence today.

    ReplyDelete