Generate Facebook Android SDK keyhash using java code


Trouble generating keyhash for Facebook SDK integration with your application, here is the java code to generate keyhash.

Add the following code inside onCreate()

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "YOUR_PACKAGE_NAME", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            System.out.println("KeyHash : "+ Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
     } catch (NameNotFoundException e) {
   } catch (NoSuchAlgorithmException e) {
 }

NOTE: Do not forget to replace YOUR_PACKAGE_NAME with your Main activity package name. After running the application you will find the key hash in LogCat.

Generate Facebook Android SDK key hash using java code
Generate Facebook Android SDK key hash using java code