Facebook为Android生成Keyhash

时间:2015-06-07 06:11:44

标签: android facebook android-studio

我遇到了在fb中生成keyhash的传统方法的问题, 这需要openssl和漫长的过程...

我想知道我是否可以输入28个随机字符并将该字符串用作我的keyhash?

2 个答案:

答案 0 :(得分:1)

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

    } catch (NoSuchAlgorithmException e) {

    }

答案 1 :(得分:0)

//Call this method for KeyHash  
    String keyHash = getKeyHashForFacebook(SplashScreen.this);
    Log.e("keyHash  ", keyHash);


    public static String getKeyHashForFacebook(Context context) {
            try {
                PackageInfo info = context.getPackageManager().getPackageInfo(
                        context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    return "KeyHash:" + context.getApplicationContext().getPackageName() + "=>"
                            + Base64.encodeToString(md.digest(), Base64.DEFAULT);
                }
            } catch (Exception e) {
    e.printStackTrace();

            }
            return "=>";
        }

@fuad为Logcat http://developer.android.com/tools/help/logcat.html检查一下![查看Logcat for KeyHash 1

enter image description here

Logcat