0

我已经开发了具有多种构建风格的应用程序,例如 dev、test、prod,所以当我使用短信验证 api 时,我只能在 prod 模式下读取短信,但不能在其他模式下读取,我已经验证了正在发送到的哈希服务器因不同的构建风格而异,服务器也在应用程序发送的短信中发送相同的哈希,但问题是短信验证 api 接收器能够读取产品构建风格的短信,而不是其他构建味道。

构建风味代码

flavorDimensions "version"

productFlavors {

    gamma {
        dimension "version"
        applicationId "packagename.test"
        versionNameSuffix "-test"
    }

    api2 {
        dimension "version"
        applicationId "packagename.dev"
        versionNameSuffix "-dev"
    }
    prod {
        dimension "version"
        applicationId "packagename"
    }
}

生成哈希的代码

   public ArrayList<String> getAppSignatures() {
    ArrayList<String> appSignaturesHashs = new ArrayList<>();
try {
        // Get all package details
        String packageName = getPackageName();
        PackageManager packageManager = getPackageManager();
        Signature[] signatures = packageManager.getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES).signatures;

        for (Signature signature : signatures) {
            String hash = hash(packageName, signature.toCharsString());
            if (hash != null) {
                appSignaturesHashs.add(String.format("%s", hash));
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Package not found", e);
    }
    return appSignaturesHashs;
}

@TargetApi(19)
private static String hash(String packageName, String signature) {
    String appInfo = packageName + " " + signature;
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(HASH_TYPE);
        messageDigest.update(appInfo.getBytes(StandardCharsets.UTF_8));
        byte[] hashSignature = messageDigest.digest();

        // truncated into NUM_HASHED_BYTES
        hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
        // encode into Base64
        String base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING | Base64.NO_WRAP);
        base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR);

        return base64Hash;
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "No Such Algorithm Exception", e);
    }
    return null;
}

任何建议或帮助都会很棒。

4

0 回答 0