Skip to content

Commit e5e2be7

Browse files
committed
Used SecureRandom to generate random ID
1 parent bb17387 commit e5e2be7

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import java.util.List;
4141
import java.util.Map;
4242
import java.util.Map.Entry;
4343
import java.util.Properties;
44-
import java.util.Random;
4544
import java.util.TimeZone;
4645
import java.util.concurrent.TimeUnit;
4746
import java.util.regex.Matcher;
@@ -181,7 +180,7 @@ public class ApiClient {
181180
httpClient = classHttpClient.newBuilder()
182181
.retryOnConnectionFailure(true)
183182
.addInterceptor(new RetryInterceptor(this.apiRequestMetrics))
184-
.eventListener(new NetworkEventListener(new Random().nextLong(), System.nanoTime()))
183+
.eventListener(new NetworkEventListener(this.getNewRandomId(), System.nanoTime()))
185184
.build();
186185
187186
verifyingSsl = true;
@@ -269,15 +268,15 @@ public class ApiClient {
269268
.proxyAuthenticator(proxyAuthenticator)
270269
.retryOnConnectionFailure(true)
271270
.addInterceptor(new RetryInterceptor(this.apiRequestMetrics))
272-
.eventListener(new NetworkEventListener(new Random().nextLong(), System.nanoTime()))
271+
.eventListener(new NetworkEventListener(this.getNewRandomId(), System.nanoTime()))
273272
.build();
274273

275274
this.setHttpClient(httpClient);
276275
}
277276

278277
this.merchantConfig = merchantConfig;
279-
RetryInterceptor.retryDelay = merchantConfig.getRetryDelay();
280-
RetryInterceptor.retryEnabled = merchantConfig.isRetryEnabled();
278+
// RetryInterceptor.retryDelay = merchantConfig.getRetryDelay();
279+
// RetryInterceptor.retryEnabled = merchantConfig.isRetryEnabled();
281280
}
282281

283282
/**
@@ -1701,4 +1700,20 @@ public class ApiClient {
17011700
throw new AssertionError(e);
17021701
}
17031702
}
1703+
1704+
private long getNewRandomId() {
1705+
SecureRandom random = new SecureRandom();
1706+
1707+
byte sessBytes[] = new byte[32];
1708+
1709+
random.nextBytes(sessBytes);
1710+
1711+
long randomId = 0;
1712+
for (int i = 0; i < sessBytes.length; i++)
1713+
{
1714+
randomId += ((long) sessBytes[i] & 0xffL) << (8 * i);
1715+
}
1716+
1717+
return randomId;
1718+
}
17041719
}

src/main/java/Invokers/ApiClient.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.util.Map;
4242
import java.util.Map.Entry;
4343
import java.util.Properties;
44-
import java.util.Random;
4544
import java.util.TimeZone;
4645
import java.util.concurrent.TimeUnit;
4746
import java.util.regex.Matcher;
@@ -181,7 +180,7 @@ public ApiClient() {
181180
httpClient = classHttpClient.newBuilder()
182181
.retryOnConnectionFailure(true)
183182
.addInterceptor(new RetryInterceptor(this.apiRequestMetrics))
184-
.eventListener(new NetworkEventListener(new Random().nextLong(), System.nanoTime()))
183+
.eventListener(new NetworkEventListener(this.getNewRandomId(), System.nanoTime()))
185184
.build();
186185

187186
verifyingSsl = true;
@@ -269,15 +268,13 @@ public Request authenticate(Route route, Response response) throws IOException {
269268
.proxyAuthenticator(proxyAuthenticator)
270269
.retryOnConnectionFailure(true)
271270
.addInterceptor(new RetryInterceptor(this.apiRequestMetrics))
272-
.eventListener(new NetworkEventListener(new Random().nextLong(), System.nanoTime()))
271+
.eventListener(new NetworkEventListener(this.getNewRandomId(), System.nanoTime()))
273272
.build();
274273

275274
this.setHttpClient(httpClient);
276275
}
277276

278277
this.merchantConfig = merchantConfig;
279-
RetryInterceptor.retryDelay = merchantConfig.getRetryDelay();
280-
RetryInterceptor.retryEnabled = merchantConfig.isRetryEnabled();
281278
}
282279

283280
/**
@@ -1701,4 +1698,20 @@ private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityExcepti
17011698
throw new AssertionError(e);
17021699
}
17031700
}
1701+
1702+
private long getNewRandomId() {
1703+
SecureRandom random = new SecureRandom();
1704+
1705+
byte sessBytes[] = new byte[32];
1706+
1707+
random.nextBytes(sessBytes);
1708+
1709+
long randomId = 0;
1710+
for (int i = 0; i < sessBytes.length; i++)
1711+
{
1712+
randomId += ((long) sessBytes[i] & 0xffL) << (8 * i);
1713+
}
1714+
1715+
return randomId;
1716+
}
17041717
}

0 commit comments

Comments
 (0)