Android Get App Signature

A while ago, I was developing an Android app, and I wanted to integrate this app with Facebook. I followed the Facebook SDK useless documentation and of-course I faced several problems.

One of these problems was at the beginning of creating the Facebook App, I was asked to enter my "Android App Key Hash". They way for getting this Hash Key is to use the key-store tool, but for some reason the key was not accepted by Facebook.

Other solution for this is to use java code to get the hash key, here is the code I used:

public class TestActivity extends Activity{
private final static String TAG = "TestActivity";//Type your activity name.
private final static Strring PACKAGE_NAME = "my.package.name" //Package Name
.
.
.
.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

.
.
.
getAppSignature();
}

private void getAppSignature(){
try{
PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, PackageManager.GET_SIGNATURES);
for(Signature signature : info.signatures){
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i(TAG, Base64.encodeToString(md.digest(), 0));  //Log the hash key
}
}catch (Exception e) {   
       Log.i(TAG, "Some error  " + e);              
       e.printStackTrace();          
}     
} 
}

Make sure to replace PACKAGE_NAME with your app package name.

Check your app log and you should see the Hash Key.