Skip to content

Commit

Permalink
use long instead of BigInteger (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Bohdan Akimenko <bohdan.akimenko@kevychsolutions.com>
  • Loading branch information
vazarkevych and Bohdan-Kim authored May 7, 2024
1 parent 9513f72 commit d2d88cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
18 changes: 9 additions & 9 deletions lib/src/main/java/growthbook/sdk/java/GrowthBookUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ class GrowthBookUtils {
}

private static Float hashV1(String stringValue, String seed) {
BigInteger bigInt = MathUtils.fnv1a_32((stringValue + seed).getBytes());
BigInteger thousand = new BigInteger("1000");
BigInteger remainder = bigInt.remainder(thousand);
long hashValue = MathUtils.fnv1a_32((stringValue + seed).getBytes());
long thousand = 1000;
long remainder = hashValue % thousand;

float remainderAsFloat = Float.parseFloat(remainder.toString());
float remainderAsFloat = Float.parseFloat(String.valueOf(remainder));
return remainderAsFloat / 1000f;
}

private static Float hashV2(String stringValue, String seed) {
BigInteger first = MathUtils.fnv1a_32((seed + stringValue).getBytes());
BigInteger second = MathUtils.fnv1a_32(first.toString().getBytes());
long first = MathUtils.fnv1a_32((seed + stringValue).getBytes());
long second = MathUtils.fnv1a_32(String.valueOf(first).getBytes());

BigInteger tenThousand = new BigInteger("10000");
BigInteger remainder = second.remainder(tenThousand);
long tenThousand = 10000;
long remainder = second % tenThousand;

float remainderAsFloat = Float.parseFloat(remainder.toString());
float remainderAsFloat = Float.parseFloat(String.valueOf(remainder));
return remainderAsFloat / 10000f;
}

Expand Down
20 changes: 10 additions & 10 deletions lib/src/main/java/growthbook/sdk/java/MathUtils.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package growthbook.sdk.java;

import java.math.BigInteger;
import java.util.List;

class MathUtils {
private static final BigInteger INIT32 = new BigInteger("811c9dc5", 16);
private static final BigInteger PRIME32 = new BigInteger("01000193", 16);
private static final BigInteger MOD32 = new BigInteger("2").pow(32);
private static final long INIT32 = 0x811c9dc5L;
private static final long PRIME32 = 0x01000193L;
private static final long MOD32 = 1L << 32;

/**
* Fowler-Noll-Vo algorithm
* fnv32a returns an integer, so we convert that to a float using a modulus
* fnv32a returns an long, so we convert that to a float using a modulus
*
* @param data byte list
* @return BigInteger
* @return long
*/
public static BigInteger fnv1a_32(byte[] data) {
BigInteger hash = INIT32;
public static long fnv1a_32(byte[] data) {
long hash = INIT32;

for (byte b : data) {
hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
hash = hash.multiply(PRIME32).mod(MOD32);
hash ^= (b & 0xff);
hash *= PRIME32;
hash %= MOD32;
}

return hash;
Expand Down

0 comments on commit d2d88cc

Please sign in to comment.