Skip to content

Commit

Permalink
Change DigestUtils.sha256Hex to MessageDigest sha256 to fix commons-c…
Browse files Browse the repository at this point in the history
…odec problem under Android API 28. (#54)
  • Loading branch information
novalisdenahi authored Jun 14, 2024
1 parent d2e5904 commit a2b6c6b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=10.2.0
version=10.2.1

org.gradle.jvmargs=-Xmx2g
36 changes: 34 additions & 2 deletions src/main/java/com/configcat/RolloutEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/configcat/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit a2b6c6b

Please sign in to comment.