Skip to content

Commit ff84463

Browse files
committed
add option to randomize RandomData seed
- if the System property `com.licel.jcardsim.randomdata.seed` is set, the hex-decoded value of the property is added as a seed material to the RandomData on initialization - else if the System property `com.licel.jcardsim.randomdata.secure` is set to `1`, the SecureRandom is used to generate 32 random bytes that are added as a seed material to the RandomData - else the original behavior is preserved to be consistent with previous versions (some tests might rely on the fixed randomness)
1 parent 0984e9c commit ff84463

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.bouncycastle.crypto.digests.SHA1Digest;
2222
import org.bouncycastle.crypto.prng.DigestRandomGenerator;
2323
import org.bouncycastle.crypto.prng.RandomGenerator;
24+
import org.bouncycastle.util.encoders.Hex;
25+
26+
import java.security.SecureRandom;
2427

2528
/**
2629
* Implementation <code>RandomData</code> based
@@ -34,6 +37,18 @@ public class RandomDataImpl extends RandomData {
3437
public RandomDataImpl(byte algorithm) {
3538
this.algorithm = algorithm;
3639
this.engine = new DigestRandomGenerator(new SHA1Digest());
40+
41+
final String randomSeed = System.getProperty("com.licel.jcardsim.randomdata.seed");
42+
final String doSecureRandom = System.getProperty("com.licel.jcardsim.randomdata.secure", "0");
43+
if (randomSeed != null){
44+
this.engine.addSeedMaterial(Hex.decode(randomSeed));
45+
}
46+
else if ("1".equals(doSecureRandom)){
47+
byte[] seed = new byte[32];
48+
SecureRandom randomGenerator = new SecureRandom();
49+
randomGenerator.nextBytes(seed);
50+
this.engine.addSeedMaterial(seed);
51+
}
3752
}
3853

3954
public void generateData(byte[] buffer, short offset, short length) throws CryptoException {

0 commit comments

Comments
 (0)