Add some logging to LinuxSecureRandom.

This commit is contained in:
Andreas Schildbach
2014-12-10 16:10:27 +01:00
parent 5aaf6a25db
commit 4499e0f7c1

View File

@@ -25,6 +25,9 @@ import java.security.Provider;
import java.security.SecureRandomSpi; import java.security.SecureRandomSpi;
import java.security.Security; import java.security.Security;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* A SecureRandom implementation that is able to override the standard JVM provided implementation, and which simply * A SecureRandom implementation that is able to override the standard JVM provided implementation, and which simply
* serves random numbers by reading /dev/urandom. That is, it delegates to the kernel on UNIX systems and is unusable on * serves random numbers by reading /dev/urandom. That is, it delegates to the kernel on UNIX systems and is unusable on
@@ -41,6 +44,8 @@ public class LinuxSecureRandom extends SecureRandomSpi {
} }
} }
private static final Logger log = LoggerFactory.getLogger(LinuxSecureRandom.class);
static { static {
try { try {
File file = new File("/dev/urandom"); File file = new File("/dev/urandom");
@@ -48,9 +53,16 @@ public class LinuxSecureRandom extends SecureRandomSpi {
// This stream is deliberately leaked. // This stream is deliberately leaked.
urandom = new FileInputStream(file); urandom = new FileInputStream(file);
// Now override the default SecureRandom implementation with this one. // Now override the default SecureRandom implementation with this one.
Security.insertProviderAt(new LinuxSecureRandomProvider(), 1); int position = Security.insertProviderAt(new LinuxSecureRandomProvider(), 1);
if (position != -1)
log.info("Secure randomness will be read from {} only.", file);
else
log.info("Randomness is already secure.");
} else { } else {
urandom = null; urandom = null;
log.info("Does not exist: {}", file);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// Should never happen. // Should never happen.