Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SecureRandom securely random #141

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import org.bouncycastle.crypto.prng.DigestRandomGenerator;
import org.bouncycastle.crypto.prng.RandomGenerator;

import java.security.SecureRandom;

/**
* Implementation <code>RandomData</code> based
* on BouncyCastle CryptoAPI.
*
* Note: SecureRandom may block on *nix due to low entropy. If necessary, configure the JVM to use /dev/urandom or egd.
* @see RandomData
*/
public class RandomDataImpl extends RandomData {
Expand All @@ -34,6 +38,14 @@ public class RandomDataImpl extends RandomData {
public RandomDataImpl(byte algorithm) {
this.algorithm = algorithm;
this.engine = new DigestRandomGenerator(new SHA1Digest());

// ALG_SECURE_RANDOM should not be consistent with each run
if (ALG_SECURE_RANDOM == algorithm) {
SecureRandom randomGenerator = new SecureRandom();
byte[] seed = new byte[32];
randomGenerator.nextBytes(seed);
this.engine.addSeedMaterial(seed);
}
}

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