Key Hash for Facebook Login implementation in Android
For implementing Facebook login in android app, you need this keyhash value. For seeing this blog you will generate the keyhash value programatically.Create a new project in android studio.
Next, select target device
After that select activity
And then click finish button.
Now, Open your MainActivity.java file and write this code.
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.you.name", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String something = new String(Base64.encode(md.digest(), 0));
//String something = new String(Base64.encodeBytes(md.digest()));
Log.e("hash key", something);
}
} catch (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());
}
Full Code:
MainActivity.java
public class MainActivity extends Activity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filed_activity);
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"Package name", //Replace your package name here
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 | NoSuchAlgorithmException e) {
}
}
}
Then, run the project you will able to see the keyhash value in your android monitor(Log).
For youtube video see the below link
https://www.youtube.com/watch?v=PHddSXeBfk8