I\'m working on an Android app, in which I want to integrate a Facebook posting feature. I downloaded the Facebook-Android SDK, and I got the readme.md (text file) in there,
The instructions currently in Facebook's Android Tutorial do not work well under Windows. Their example shows how to pipe the keytool output to openssl but if you try this under Windows the output is not valid for some reason. I found that I had to use intermediary files to get it to work properly. Here are the steps that worked for me:
Start by downloading openssl for Windows from Google.
C:\Users\Me>keytool -exportcert -alias my_key -keystore my.keystore -storepass PASSWORD > mycert.bin
C:\Users\Me>openssl sha1 -binary mycert.bin > sha1.bin
C:\Users\Me>openssl base64 -in sha1.bin -out base64.txt
After running these commands the valid hash is stored in the file base64.txt. Copy and paste this to your app settings on Facebook.
I have done by this way for Linux OS & Windows OS:
keytool -exportcert -alias **myaliasname** -keystore **/home/comp-1/Desktop/mykeystore.jks** | openssl sha1 -binary | openssl base64
Kindly change Alias Name and Keystore with it's path as your requirement.
Terminal would ask for Password of Keystore. You have to provide password for the same Keystore.
So finally you would get the Release Hashkey.
Steps for Release Hashkey:
keytool -exportcert -alias **myaliasname** -keystore **"C:\Users\hiren.patel\Desktop\mykeystore.jks"** | "C:\openssl-0.9.8e_X64\bin\openssl.exe" sha1 -binary | "C:\openssl-0.9.8e_X64\bin\openssl.exe" base64
Kindly change Alias Name and Keystore with it's path as your requirement.
Terminal would ask for Password of Keystore. You have to provide password for the same Keystore.
So finally you would get the Release Hashkey.
Done
As answered on a similar issue i found this to be working for me:
apkname.apk
file you want to know the hash of to the 'Java\jdk1.7.0_79\bin' folderkeytool -list -printcert -jarfile apkname.apk
SHA1
value and convert it using this siteThe best approach is to use the following code:
private void getHashKey(String pkgName)
{
try
{
PackageInfo info = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures)
{
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String hashKey = Base64.encodeBytes(md.digest());
_hashKey_et.setText(hashKey);
Log.i("KeyTool", pkgName + " -> hashKey = " + hashKey);
}
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
But I was so frustrating with the fact that there is no simple tool to generate the HashKey for the Facebook app. Each time I had to play with Openssl and Keytool or to use a code to get the hash from signature ...
So I wrote a simple KeyGenTool that will do that work for you: -> KeyGenTool on Google Play <-
Enjoy :)
There are two methods available complex one and the easy one
Methods One :(little Complex)
first of all you have to download ssl 64bit
or 32bit
accordingly, remember to download the file with name containing e
after version code openssl-0.9.8e_X64.zip OR openssl-0.9.8e_WIN32.zip not with the k
after the version code,
and place in AndroidStudio/jre/bin directory, if you dont know where to place, you can find this directory by right clicking on the android studio shortcut as:
now you have managed two required things in one place, but still you have to find the path for your debug.keystore
, that is always can be found in the "C:\Users\yourusernamehere\.android\debug.keystore"
,
NOTE If your app is published already, or about to publish then use your publishing signing keystore, if and only if your are testing in development mode than you can use debug,keysotre
As everything is setup, let arrange the command you wanted to execute for the hash key generation in base64 format
, and you command will look like this
keytool.exe -exportcert -alias androiddebugkey -keystore "C:\Users\ayyaz talat\.android\debug.keystore" | "D:\Program Files\Android\Android Studio\jre\bin\openssl\bin\openssl.exe" sha1 -binary |"D:\Program Files\Android\Android Studio\jre\bin\openssl\bin\openssl.exe" base64
it will promt you to enter a password for the debug.keystore, which is android by default. if you are using your own key than the password will be also yours. the output will look like this if everything goes well as expected, hope it may help
Second Method (Respectively easy one)
if you dont want to go throught all the above procedure, then just use the following method to log the haskey:
private void printKeyHash() {
try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("KeyHash:", e.toString());
} catch (NoSuchAlgorithmException e) {
Log.e("KeyHash:", e.toString());
}
}
output:
The simplest solution I have found is this:
Look for the line in the log that looks like this:
04-24 01:14:08.605: I/System.out(31395): invalid_key:Android key mismatch.
Your key "abcdefgHIJKLMN+OPqrstuvwzyz" does not match the allowed keys specified in your
application settings. Check your application settings at
http://www.facebook.com/developers
Copy "abcdefgHIJKLMN+OPqrstuvwzyz" and paste it into the Facebook Android Key Hash area.