From a2b6c6b895440ea5812ad03d6d5549aacae1a82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3th=20D=C3=A1niel?= Date: Fri, 14 Jun 2024 15:05:15 +0200 Subject: [PATCH] Change DigestUtils.sha256Hex to MessageDigest sha256 to fix commons-codec problem under Android API 28. (#54) --- gradle.properties | 2 +- .../java/com/configcat/RolloutEvaluator.java | 36 +++++++++++++++++-- src/main/java/com/configcat/Utils.java | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 8ac16fe..042ff62 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -version=10.2.0 +version=10.2.1 org.gradle.jvmargs=-Xmx2g \ No newline at end of file diff --git a/src/main/java/com/configcat/RolloutEvaluator.java b/src/main/java/com/configcat/RolloutEvaluator.java index ef346a7..65d1371 100644 --- a/src/main/java/com/configcat/RolloutEvaluator.java +++ b/src/main/java/com/configcat/RolloutEvaluator.java @@ -4,6 +4,8 @@ import org.apache.commons.codec.digest.DigestUtils; import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.*; class EvaluationResult { @@ -28,6 +30,9 @@ class RolloutEvaluator { public static final String COMPARISON_OPERATOR_IS_INVALID = "Comparison operator is invalid."; public static final String CANNOT_EVALUATE_THE_USER_INVALID = " attribute is invalid ("; public static final String COMPARISON_VALUE_IS_MISSING_OR_INVALID = "Comparison value is missing or invalid."; + + private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); + private final ConfigCatLogger logger; public RolloutEvaluator(ConfigCatLogger logger) { @@ -519,7 +524,7 @@ private boolean evaluateContainsAnyOf(UserCondition userCondition, String userVa private static String getSaltedUserValue(String userValue, String configJsonSalt, String contextSalt) { - return DigestUtils.sha256Hex(userValue + configJsonSalt + contextSalt); + return sha256(userValue + configJsonSalt + contextSalt); } private static String getSaltedUserValueSlice(byte[] userValueSliceUTF8, String configJsonSalt, String contextSalt) { @@ -530,7 +535,34 @@ private static String getSaltedUserValueSlice(byte[] userValueSliceUTF8, String System.arraycopy(userValueSliceUTF8, 0, concatByteArrays, 0, userValueSliceUTF8.length); System.arraycopy(configSaltByteArray, 0, concatByteArrays, userValueSliceUTF8.length, configSaltByteArray.length); System.arraycopy(contextSaltByteArray, 0, concatByteArrays, userValueSliceUTF8.length + configSaltByteArray.length, contextSaltByteArray.length); - return DigestUtils.sha256Hex(concatByteArrays); + return sha256(concatByteArrays); + } + + private static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + int v; + for (int j = 0; j < bytes.length; j++) { + v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; + } + return new String(hexChars); + } + + private static String sha256(String text){ + byte[] textBytes = text.getBytes(StandardCharsets.UTF_8); + return sha256(textBytes); + } + + private static String sha256(byte[] byteArray){ + MessageDigest digest = null; + try { + digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(byteArray); + return bytesToHex(hash); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } } private boolean evaluateSegmentCondition(SegmentCondition segmentCondition, EvaluationContext context, String configSalt, Segment[] segments, EvaluateLogger evaluateLogger) { diff --git a/src/main/java/com/configcat/Utils.java b/src/main/java/com/configcat/Utils.java index 1bf3e11..173bd3d 100644 --- a/src/main/java/com/configcat/Utils.java +++ b/src/main/java/com/configcat/Utils.java @@ -30,7 +30,7 @@ private Constants() { /* prevent from instantiation*/ } static final long DISTANT_PAST = 0; static final String CONFIG_JSON_NAME = "config_v6.json"; static final String SERIALIZATION_FORMAT_VERSION = "v2"; - static final String VERSION = "10.2.0"; + static final String VERSION = "10.2.1"; static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/"; static final String SDK_KEY_PREFIX = "configcat-sdk-1";