42
42
final class ConditionEvaluator {
43
43
private static final int MAX_CONDITION_RECURSION_DEPTH = 10 ;
44
44
private static final Logger logger = LoggerFactory .getLogger (ConditionEvaluator .class );
45
+ private static final BigInteger MICRO_PERCENT_MODULO = BigInteger .valueOf (100_000_000L );
45
46
46
47
/**
47
48
* Evaluates server conditions and assigns a boolean value to each condition.
@@ -226,8 +227,7 @@ private BigInteger getMicroPercentile(String seed, String randomizationId) {
226
227
String seedPrefix = seed != null && !seed .isEmpty () ? seed + "." : "" ;
227
228
String stringToHash = seedPrefix + randomizationId ;
228
229
BigInteger hash = hashSeededRandomizationId (stringToHash );
229
- BigInteger modValue = new BigInteger (Integer .toString (100 * 1_000_000 ));
230
- BigInteger microPercentile = hash .mod (modValue );
230
+ BigInteger microPercentile = hash .mod (MICRO_PERCENT_MODULO );
231
231
232
232
return microPercentile ;
233
233
}
@@ -238,19 +238,8 @@ private BigInteger hashSeededRandomizationId(String seededRandomizationId) {
238
238
MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
239
239
byte [] hashBytes = digest .digest (seededRandomizationId .getBytes (StandardCharsets .UTF_8 ));
240
240
241
- // Convert the hash bytes to a hexadecimal string.
242
- StringBuilder hexString = new StringBuilder ();
243
- for (byte b : hashBytes ) {
244
- String hex = Integer .toHexString (0xff & b );
245
- if (hex .length () == 1 ) {
246
- hexString .append ('0' );
247
- }
248
- hexString .append (hex );
249
- }
250
-
251
- // Convert the hexadecimal string to a BigInteger
252
- return new BigInteger (hexString .toString (), 16 );
253
-
241
+ // Convert the hash bytes to a BigInteger
242
+ return new BigInteger (1 , hashBytes );
254
243
} catch (NoSuchAlgorithmException e ) {
255
244
logger .error ("SHA-256 algorithm not found" , e );
256
245
throw new RuntimeException ("SHA-256 algorithm not found" , e );
0 commit comments