Error while creating a hash key - android

C:\Documents and Settings\Admin\ keytool -export -alias androiddebugkey -keystore "C:\Documents and Settings\Admin\.android\debug.keystore" | D:\openssl\bin\openssl.exe sha1 -binary | D:\openssl\bin\openssl.exe enc -a
I just run the above code for generating the hash key but it shows some errors...like
The filename ,directory name,or volume label syntax is incorrect

You may check this link below for step by step tutorial
How to get Key Hashes for android-facebook app
If you still have the same problem then you may use the below code snippet to generate keyhash. This works perfectly fine for me.
PackageInfo packageInfo;
try {
packageInfo = getPackageManager().getPackageInfo("com.yourapp",
PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String key = new String(Base64.encode(md.digest(), 0));
// String key = new String(Base64.encodeBytes(md.digest()));
Log.e("Hash key", key);
}
}
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());
}

Related

Invalid key hash

Hi I'm very new to facebook login. I tried to generate a hash key for facebook android app. The first time it didn't asked for a password and gave me the hash key but now when I again tried to generate the hash key its asking for pass. When I enter "android" as pass, it gives me just DrNQ.
command used:
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\Sam\.android\debug.keystore" | "C:\Openssl\bin\openssl" sha1 -binary | "C:\Openssl\bin\openssl" base64
you are using to much complex trick to get hash key of your application it's having lots of chance to get error.But I have one simple idea to generate your application hash key.
for generating hash key you must need to specify your package name and you are just putting following code while app is lunch and you are getting yout app hash key in Log.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.example.chirag.maptesting",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
Toast.makeText(Main2Activity.this, "Hash Key :"+Base64.encodeToString(md.digest(), Base64.DEFAULT), Toast.LENGTH_SHORT).show();
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
Note :
in Above code com.example.chirag.maptesting is my application package name but you must need to paste your package name here.
I hope your are clear with my idea.
Best Luck
Using below code snippet, you can get the hash code.
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 (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 (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;
}

Command line gives different hash key for signKey.jks

According to facebook sdk I used
keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64
to get the hash key. But I got error while login that hash key did not matched. I am using the same apk signed same signing key. Then I used
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());
}
It is giving me the actual hash key. My Question is why hash key generated from command line is different than from code when I am using the same .jks(key) file.

Invalid Hash key in facebook app in android

I got hash key from below code in android:
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
}
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());
}
But it was invalid key... Please suggestion how to verify hash key for Facebook login....
You are getting the hash-key with debug key... Which may work if you haven't sign the package and running app in debug mode. What you need to do is :
1) Go to the manifest file and add to the application android:debuggable="true".
2) Sign the application, capy and install to your device manually or for use "adb install path_to_apk" from command line.
3) Now run your app and monitor the logcat.
4) You will get printed a new key which will be the matching key with key facebook app is showing in the error msg, The key you have got is now having a = sign in the last.
5) Register this key on facebook developer site
Alternate Trick
You can do one other thing Simply register key which is showing in the error msg "The key hash ### does not match any stored key hashes" Just add the = in the end of the ###. It will be like ###=
you are done!! Hope this will work.
Add this function into your class,and call this function in oncreate methode,then generate sign apk and run sign apk in your device and check log-cat, copy generated hash key to facebook developer console.
private void showHashKey()
{
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.kisan.kisan",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
Check facebook authentication,it will work fine
I think the issue is you might not have updated the keyhash in the facebook developer account. Refer the: https://developers.facebook.com/docs/android/getting-started
Generate the Keyhash using:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
And update into your facebook devloper site, under Apps tab, General Settings
Assign this to a button's on click and get the Key Hash from the logcat.
Button getKeyHash = (Button) findViewById(R.id.button_key_hash);
getKeyHash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Key Hash
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} 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());
}
}
});
Now go to your app at https://developers.facebook.com/apps
. Then click on settings, add your Key Hash and save.

Invalid Key hash with Facebook Android SDK

I'm trying to use Facebook Android SDK to develop a simple app with the Facebook Login Button.
But i'm having trouble with Key Hashes. I've created both a debug key and a release key:
Debug key:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
Release key:
keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64
And then i copied this key hashes in the Facebook Developers page.
When i export the apk and copy it into the device it works well, but when i try to install the app from Eclipse (run as/debug as Android Application) it doesn't work. It seems that the app is using a different key hash from the one i've created with keytool.
Anyone knows how to solve this problem?
Try to get the HashKey from here
public static void showHashKey(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
"com.example.tryitonjewelry", PackageManager.GET_SIGNATURES); //Your package name here
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
try {
PackageInfo info = getPackageManager().getPackageInfo("your pakage name here", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.e("KeyHash:", "key is: "+Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
Log.e("error","error name not found");
} catch (NoSuchAlgorithmException e) {
Log.e("error","error no algorithm");
}**strong text**
By using this one u can get ur key hash and then use this one in facebook devloper site.
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());
Log.d("KeyHash:", "KeyHash: " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
}
catch (PackageManager.NameNotFoundException e) {
}
catch (NoSuchAlgorithmException e) {
}

Generate hash key for android mobile app

I am working on a android app and has to provide the hashkey to facebook. I have tried 3 appoarch but none of them works.
By following the official doc:
1.) I write a command in windows like this
"C:\Program Files\Java\jdk1.7.0_17\bin\keytool.exe" -exportcert -alias androiddebugkey -keystore C:\hk7.keystore | "C:\openssl\bin\openssl.exe" sha1 -binary | "C:\openssl\bin\openssl.exe" base64
output:
57GY9jCxTL9lwhI9oAsjNZN1UJ4=
2.) And the programatically way
try {
PackageInfo info = getPackageManager().getPackageInfo("com.package.mypackage", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String sign=Base64.encodeToString(md.digest(), Base64.DEFAULT);
Log.e("MY KEY HASH:", sign);
Toast.makeText(getApplicationContext(),sign, Toast.LENGTH_LONG).show();
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
output:
PH//aR6qmcnZxj/n+4oT6AzEMZQ=
Works but only for my device
3.) the alias value
keytool -alias hk7 -exportcert -keystore C:\hk7.keystore | "C:\openssl\bin\openssl.exe" sha1 -binary | "C:\openssl\bin\openssl.exe" base64
output:
kiHnmqD3oxggF1Y7rgTqaWTPZk4=
Only the second way works but it is only for my device . In my app , I log the exception , it said the API key does not match . but it seems each device has its own key. How to fix that? and is it caused my openssl is too old? or the android side implementation problem? Are there any site for testing whether the key is work? Thanks for helping
2nd way is t best one, Now you need to add every device hashkey on which you want to test as your app is not on playstore .. once you upload t app on playstore/sign with a perticular keystore then you have to add only that keyhash.... Use this function to get KEYHASH
private void printKeyHashForThisDevice() {
try {
PackageInfo info = getPackageManager().getPackageInfo("com.package", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
Logger.logger("============================================");
Logger.logger("KeyHash================ ", keyHash);
Logger.logger("============================================");
System.out.println("KeyHash================ " + keyHash);
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}

Categories

Resources