Skip to content

Commit 2d72f46

Browse files
Merge branch 'fix-sdk-use-secure-random' into 'master'
[Random] Provide initialized secure random instead of seed See merge request codingame/game-engine!310
2 parents bc07c06 + 68d8a73 commit 2d72f46

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

engine/core/src/main/java/com/codingame/gameengine/core/MultiplayerGameManager.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.io.IOException;
44
import java.io.StringReader;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.security.SecureRandom;
57
import java.util.List;
68
import java.util.Properties;
9+
import java.util.Random;
710
import java.util.Scanner;
811
import java.util.Map.Entry;
912
import java.util.concurrent.ThreadLocalRandom;
@@ -21,8 +24,11 @@
2124
@Singleton
2225
public final class MultiplayerGameManager<T extends AbstractMultiplayerPlayer> extends GameManager<T> {
2326

27+
private static final String RANDOM_SECURE_SHA1PRNG_ALGORITHM = "SHA1PRNG";
28+
2429
private Properties gameParameters;
2530
private long seed;
31+
private Random random;
2632

2733
@Override
2834
protected void readGameProperties(InputCommand iCmd, Scanner s) {
@@ -46,6 +52,15 @@ protected void readGameProperties(InputCommand iCmd, Scanner s) {
4652
}
4753
}
4854
gameParameters.setProperty("seed", String.valueOf(seed));
55+
56+
try {
57+
// This random generator is reproducible whereas default one (native PRNG is not)
58+
random = SecureRandom.getInstance(RANDOM_SECURE_SHA1PRNG_ALGORITHM);
59+
random.setSeed(seed);
60+
} catch (NoSuchAlgorithmException e1) {
61+
log.error("Error while creating secure random number generator. Default to non-secure random generator", e1);
62+
random = new Random(seed);
63+
}
4964
}
5065

5166
@Override
@@ -70,15 +85,29 @@ public int getPlayerCount() {
7085

7186
/**
7287
* <p>
73-
* The seed is used to generated parameters such as width and height.<br>
74-
* If a seed is present in the given input, the input value should override the generated values.
88+
* The seed is used to initialize the Random number generator.<br>
89+
* If a seed is present in the given input, the input value should override the generated values.<br>
90+
*
91+
* The seed should NOT be used directly in referee but through the random number generator provided by @method getRandom
7592
* </p>
7693
*
7794
* @return an <code>long</code> containing a given or generated seed.
7895
*/
7996
public long getSeed() {
8097
return seed;
8198
}
99+
100+
/**
101+
* <p>
102+
* The random generator is used to generated parameters such as width and height.<br>
103+
* The provided random generator is a SecureRandom using the SHAPRNG algorithm.<br>
104+
* </p>
105+
*
106+
* @return an <code>Random</code> containing a given or generated seed.
107+
*/
108+
public Random getRandom() {
109+
return random;
110+
}
82111

83112
/**
84113
* <p>

playground/misc/misc-3-release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
88

99
- Updated links to pixi docs
1010
- Updated log4j dependency
11+
- Use secure random
1112

1213
## 4.1.6
1314

0 commit comments

Comments
 (0)