Skip to content

Commit

Permalink
feat: random val
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Dec 20, 2023
1 parent 45964c6 commit 4ca30d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ly.count.sdk.java.internal;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -146,7 +145,7 @@ void autoCloseRequiredViews(boolean closeAllViews, Map<String, Object> customVie
autoCloseRequiredViews(false, null);

ViewData currentViewData = new ViewData();
currentViewData.viewID = safeRandomVal();
currentViewData.viewID = Utils.safeRandomVal();
currentViewData.viewName = viewName;
currentViewData.viewStartTimeSeconds = TimeUtils.uniqueTimestampS();
currentViewData.isAutoStoppedView = viewShouldBeAutomaticallyStopped;
Expand Down Expand Up @@ -323,20 +322,6 @@ private void addSegmentationToViewWithIDInternal(String viewID, Map<String, Obje
vd.viewSegmentation.putAll(viewSegmentation);
}

/**
* Creates a crypto-safe SHA-256 hashed random value
*
* @return returns a random string value
*/
public static String safeRandomVal() {
long timestamp = System.currentTimeMillis();
SecureRandom random = new SecureRandom();
byte[] value = new byte[6];
random.nextBytes(value);
String b64Value = Utils.Base64.encode(value);
return b64Value + timestamp;
}

public class Views {

/**
Expand Down
15 changes: 15 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -387,4 +388,18 @@ public static boolean isValidURL(String url) {
return false;
}
}

/**
* Creates a crypto-safe SHA-256 hashed random value
*
* @return returns a random string value
*/
public static String safeRandomVal() {
long timestamp = System.currentTimeMillis();
SecureRandom random = new SecureRandom();
byte[] value = new byte[6];
random.nextBytes(value);
String b64Value = Utils.Base64.encode(value);
return b64Value + timestamp;
}
}
23 changes: 23 additions & 0 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ly.count.sdk.java.Config;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -559,4 +561,25 @@ public void readStream() {
Assert.assertNull(Utils.readStream(null, logger));
Assert.assertEquals(value, new String(Utils.readStream(new ByteArrayInputStream(value.getBytes()), logger)));
}

/**
* "safeRandomVal"
* An random value is generated and validated by the regex, and consist of 2 parts
* Should return the string value generated by the algorithm and matches with the regex
*
* @throws NumberFormatException for parsing part 2
*/
@Test
public void safeRandomVal() throws NumberFormatException {
String val = Utils.safeRandomVal();
Pattern pattern = Pattern.compile("^([a-zA-Z0-9]{8})(\\d+)$");

Matcher matcher = pattern.matcher(val);
if (matcher.matches()) {
Assert.assertFalse(matcher.group(1).isEmpty());
Assert.assertTrue(Long.parseLong(matcher.group(2)) > 0);
} else {
Assert.fail("No match for " + val);
}
}
}

0 comments on commit 4ca30d4

Please sign in to comment.