Java RNG - How to print out which secure random file being used from OS?

291 Views Asked by At

I have a quick question 7:34 I am not a developer but I wanted to test a scenario. This is about secure RNG. I would like to have a jar file where it just System.out.println which tell which securerandom.source is being used with the current JRE this jar file is being run. That means If I run this jar file java -jar myconfigOut.jar then it should be print to console something like

Secure Random file used = /dev/urandom (or /dev/random)   <-- which ever value is being used in that JRE.

https://docs.oracle.com/javase/9/docs/api/java/security/SecureRandom.html

I quickly searched and grabbed the oracle library wiki but have no idea how to do this coding. I am sure it will be a simple couple of lines code if we know java.

How to do this ??

1

There are 1 best solutions below

0
SAGAR BHOOSHAN On

Well I have done some exhaustive searching and learnt quick basics of java in an hour and wrote a code that actually spits out the requirement. Here is the code below

import java.security.*;
import java.util.*;

public class myConfigOut {
    public static void main(String[] argv) {

        try {
            // Trying to see which secureRandom provider we are using
            System.out.println("Trying to output RNG source");
            SecureRandom secureRandom = new SecureRandom();
            System.out.println("Secure random source: " + Security.getProperty("securerandom.source"));
            System.out.println("java.security.egd: " + System.getProperty("java.security.egd"));
            System.out.println("Algorithm: " + secureRandom.getAlgorithm());
        } finally {
            System.out.println("I'm done here");
        }
    }
}

And the output looks like as follows:

Trying to output RNG source
Secure random source: file:/dev/urandom
java.security.egd: file:/dev/urandom
Algorithm: NativePRNG
I'm done here

This is quite a naive code as I just skim through how to write java hello world and some other things and then wrote this simple code. I have to write the code and/or ask here because I couldn't find this requirement anywhere but my requirement is fulfilled and all good here.