我正在开发一个Android应用程序,我想在其中集成一个Facebook 发布功能。我下载了Facebook-Android SDK,然后 readme。Md(文本文件)在那里,其中提到要生成 Android的键散列。我如何生成它?


当前回答

使用它在kotlin中打印键哈希

try {
        val info = context.getPackageManager().getPackageInfo(context.packageName,
                PackageManager.GET_SIGNATURES);
        for (signature in info.signatures) {
            val md = MessageDigest.getInstance("SHA")
            md.update(signature.toByteArray())
            Log.d("Key hash ", android.util.Base64.encodeToString(md.digest(), android.util.Base64.DEFAULT))
        }
    }catch (e:Exception){

    }

其他回答

Kotlin代码获取哈希键

 private fun logHashKey() {
    try {
        val info = getPackageManager().getPackageInfo("your.package.name", PackageManager.GET_SIGNING_CERTIFICATES);
        for (signature in info.signingInfo.signingCertificateHistory) {

            val md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            val something = Base64.getEncoder().encodeToString(md.digest());
            Log.e("hash key", something);
        }
    } catch (e1: PackageManager.NameNotFoundException) {
        Log.e("name not found", e1.toString());
    } catch (e: NoSuchAlgorithmException) {
        Log.e("no such an algorithm", e.toString());
    } catch (e: Exception) {
        Log.e("exception", e.toString());
    }
}

请不要忘记在调试和发布环境中生成键,因为它们会随着构建设置而变化。

facebook开发者网站上的官方文件:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook", 
                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 (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

将此代码添加到您的活动的onCreate,它将在您的logCat的KeyHash标签下打印散列

try {
    PackageInfo info = getPackageManager().getPackageInfo(
                           getPackageName(),
                           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 (NameNotFoundException e) {

}
catch (NoSuchAlgorithmException e) {

}

您可以为您的帐户添加多个hashkey,所以如果您一直在调试中运行,不要忘记在发布模式中再次运行此命令。

下载openSSL ->安装它->通常会安装在C:\ openSSL

然后打开CMD并键入

cd../../Program Files (Enter)

java (Enter)

dir (Enter)

cd jdk1.6.0_17 (varies with jdk versions) (Enter)

查看jdk版本到C:/program files/java/jdk_version

cd bin (enter)

keytool -exportcert -alias androiddebugkey -keystore C:Users\Shalini\.android\debug.keystore | "C:\OpenSSL\bin\openssl sha1 -binary | "C:\OpenSSL\bin\openssl base64 (Enter)

它会问你密码是android。

keytool -exportcert -alias androiddebugkey -keystore       C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64

这对我很管用……

步骤:

1) Open command line go to - > java Keytool..... for me C:\Program Files\Java\JDK1.7\bin
2) Download OpenSSL from google
3) paste this with changing your paths -
   keytool -exportcert -alias androiddebugkey -keystore C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64 

    ....................   give proper debug.keystore path and openSSL path .. 

4) Finley it may be ask u password .. so give password -> android   ...
5) you will get 28 characters that will be your has key