mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-07 07:40:55 +03:00
Added logging to trace time creation of Randoms
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,9 +59,9 @@ 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];
|
||||||
@@ -59,5 +69,6 @@ public class JCERandom
|
|||||||
System.arraycopy(tmp, 0, foo, start, len);
|
System.arraycopy(tmp, 0, foo, start, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user