Skip to content

Instantly share code, notes, and snippets.

@pranaypatel512
Created January 22, 2016 04:24
Show Gist options
  • Save pranaypatel512/b4ac0bc953e59d05f924 to your computer and use it in GitHub Desktop.
Save pranaypatel512/b4ac0bc953e59d05f924 to your computer and use it in GitHub Desktop.
This is code to generate key hash which require in facebook sdk integration for sharing and signin with facebook.
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 (android.content.pm.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment