Why keytool is generating different facebook androiddebugkey hash?

前端 未结 1 640
独厮守ぢ
独厮守ぢ 2021-01-07 14:34

I am creating Android application which use Facebook login SDK.

I\'d like to generate debug key hash. On Facebook website I found this command:

<
相关标签:
1条回答
  • 2021-01-07 15:23

    I also had a lot of problems getting the keytool to generate a valid hash, but i implemented the method below which i found, and was able to log out a valid hash. The exact origin of the code is a bit unclear, but this blog post is a good guess.

    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 (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());
        }
    
        return key;
    }
    
    0 讨论(0)
提交回复
热议问题