Added logging to trace time creation of Randoms

This commit is contained in:
Jeroen van Erp
2016-12-28 09:52:20 +01:00
parent 61af500c3e
commit fc75f9796c
2 changed files with 21 additions and 3 deletions

View File

@@ -17,6 +17,8 @@ package net.schmizz.sshj.transport.random;
import org.bouncycastle.crypto.prng.RandomGenerator; import org.bouncycastle.crypto.prng.RandomGenerator;
import org.bouncycastle.crypto.prng.VMPCRandomGenerator; import org.bouncycastle.crypto.prng.VMPCRandomGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.SecureRandom; import java.security.SecureRandom;
@@ -27,6 +29,8 @@ import java.security.SecureRandom;
public class BouncyCastleRandom public class BouncyCastleRandom
implements Random { implements Random {
private static final Logger logger = LoggerFactory.getLogger(BouncyCastleRandom.class);
/** Named factory for the BouncyCastle <code>Random</code> */ /** Named factory for the BouncyCastle <code>Random</code> */
public static class Factory public static class Factory
implements net.schmizz.sshj.common.Factory<Random> { implements net.schmizz.sshj.common.Factory<Random> {
@@ -41,7 +45,10 @@ public class BouncyCastleRandom
private final RandomGenerator random = new VMPCRandomGenerator(); private final RandomGenerator random = new VMPCRandomGenerator();
public BouncyCastleRandom() { public BouncyCastleRandom() {
logger.info("Generating random seed from SecureRandom.");
long t = System.currentTimeMillis();
byte[] seed = new SecureRandom().generateSeed(8); byte[] seed = new SecureRandom().generateSeed(8);
logger.debug("Creating random seed took {} ms", System.currentTimeMillis() - t);
random.addSeedMaterial(seed); random.addSeedMaterial(seed);
} }

View File

@@ -16,10 +16,13 @@
package net.schmizz.sshj.transport.random; package net.schmizz.sshj.transport.random;
import java.security.SecureRandom; import java.security.SecureRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** A {@link Random} implementation using the built-in {@link SecureRandom} PRNG. */ /** A {@link Random} implementation using the built-in {@link SecureRandom} PRNG. */
public class JCERandom public class JCERandom
implements Random { implements Random {
private static final Logger logger = LoggerFactory.getLogger(JCERandom.class);
/** Named factory for the JCE {@link Random} */ /** Named factory for the JCE {@link Random} */
public static class Factory public static class Factory
@@ -38,7 +41,14 @@ public class JCERandom
} }
private byte[] tmp = new byte[16]; private byte[] tmp = new byte[16];
private final SecureRandom random = new SecureRandom(); private final SecureRandom random;
JCERandom() {
logger.info("Creating new SecureRandom.");
long t = System.currentTimeMillis();
random = new SecureRandom();
logger.debug("Random creation took {} ms", System.currentTimeMillis() - t);
}
/** /**
* Fill the given byte-array with random bytes from this PRNG. * Fill the given byte-array with random bytes from this PRNG.
@@ -49,15 +59,16 @@ public class JCERandom
*/ */
@Override @Override
public synchronized void fill(byte[] foo, int start, int len) { public synchronized void fill(byte[] foo, int start, int len) {
if (start == 0 && len == foo.length) if (start == 0 && len == foo.length) {
random.nextBytes(foo); random.nextBytes(foo);
else } else {
synchronized (this) { synchronized (this) {
if (len > tmp.length) if (len > tmp.length)
tmp = new byte[len]; tmp = new byte[len];
random.nextBytes(tmp); random.nextBytes(tmp);
System.arraycopy(tmp, 0, foo, start, len); System.arraycopy(tmp, 0, foo, start, len);
} }
}
} }
} }