The following demonstrates how to use Java language to retrieve random data from a SwiftRNG device when used in LInux environment. The following code will only work when running run-swrng-pserver.sh
script or swrandom
kernel module.
A sample source code in Java for retrieving random primitive values from a SwiftRNG device.
package swiftrng;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
*
* Java example for retrieving random Java primitives from a SwiftRNG device when used with Linux.
*
*/
public class RetrieveRandomPrimitives {
public static void main(String[] args) throws IOException {
// Read from /tmp/swiftrng when using run-swrng-pserver.sh script.
// Read from /dev/swrandom when using swrandom kernel module.
DataInputStream dis = new DataInputStream(new FileInputStream("/dev/swrandom"));
try {
System.out.println("Random Integer: " + dis.readInt());
System.out.println("Random Byte: " + dis.readByte());
System.out.println("Random Unsigned Byte: " + dis.readUnsignedByte());
System.out.println("Random Double: " + dis.readDouble());
System.out.println("Random Float: " + dis.readFloat());
System.out.println("Random Long: " + dis.readLong());
System.out.println("Random Short: " + dis.readShort());
System.out.println("Random Unsigned Short: " + dis.readUnsignedShort());
} catch (IOException e) {
// TODO Handle error
e.printStackTrace();
}
dis.close();
}
}