android - 两个字符串不等于

时间:2017-03-15 08:40:37

标签: android

我使用此代码获取应用程序是否由我签名。

String SIGNATURE = "HmdQ7mF9uZ2unNb8qz1HEuD+iT4=";

    try {

        PackageInfo packageInfo = context.getPackageManager()

            .getPackageInfo(context.getPackageName(),

                            PackageManager.GET_SIGNATURES);

        for (Signature signature : packageInfo.signatures) {

            byte[] signatureBytes = signature.toByteArray();

            MessageDigest md = MessageDigest.getInstance("SHA");

            md.update(signature.toByteArray());

            final String currentSignature = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d("1",currentSignature);
            Log.d("2",SIGNATURE);
            Log.d("equals:",
                  currentSignature.equals(SIGNATURE)?"true":"false");

        }
    } catch (Exception e) {
    }

logcat包含的是:

1                                            HmdQ7mF9uZ2unNb8qz1HEuD+iT4=
2                                            HmdQ7mF9uZ2unNb8qz1HEuD+iT4=
equals                                       : false

字符串是等于但是logcat不说这个!

什么问题?

1 个答案:

答案 0 :(得分:0)

试试这个,

     if(currentSignature.trim().equals(SIGNATURE.trim()))
     {
        Log.d("TAG","equal");
     }
     else
     {
        Log.d("TAG","not equal");
     }