Skip to content

Commit

Permalink
add option to randomize RandomData seed
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
ph4r05 committed May 19, 2020
1 parent 6c0cd78 commit bccbb8b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.prng.DigestRandomGenerator;
import org.bouncycastle.crypto.prng.RandomGenerator;
import org.bouncycastle.util.encoders.Hex;

import java.security.SecureRandom;

/**
* Implementation <code>RandomData</code> based
Expand All @@ -34,6 +37,18 @@ public class RandomDataImpl extends RandomData {
public RandomDataImpl(byte algorithm) {
this.algorithm = algorithm;
this.engine = new DigestRandomGenerator(new SHA1Digest());

final String randomSeed = System.getProperty("com.licel.jcardsim.randomdata.seed");
final String doSecureRandom = System.getProperty("com.licel.jcardsim.randomdata.secure", "0");
if (randomSeed != null){
this.engine.addSeedMaterial(Hex.decode(randomSeed));
}
else if ("1".equals(doSecureRandom)){
byte[] seed = new byte[32];
SecureRandom randomGenerator = new SecureRandom();
randomGenerator.nextBytes(seed);
this.engine.addSeedMaterial(seed);
}
}

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

0 comments on commit bccbb8b

Please sign in to comment.