The following demonstrates how to use Java language to download random data from a AlphaRNG device when used with 64 bit Entropy Server for Windows.
A sample source code for retrieving an array of 10 random bytes from AlphaRNG device when using Entropy Server for Windows
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
/**
*
* A sample Java application that demonstrates how to connect to Entropy Server
* in Windows for retrieving random bytes from AlphaRNG device.
*
*
*/
public class TestAlphaRNGPipe {
/**
* @param args
*/
public static void main(String[] args) {
RandomAccessFile pipe = null;
try {
// Connect to Entropy Server pipe
pipe = new RandomAccessFile("\\\\.\\pipe\\AlphaRNG", "rw");
// Send request to Entropy Server pipe to retrieve 10 random bytes.
// Request bytes should be sent at once with no delay in between.
pipe.write(createPipeRequest(10));
// Read 10 random bytes from the response.
// Random bytes should be received at once with no delay in between.
byte[] bytesToReceive = new byte[10];
int retrievedCount = pipe.read(bytesToReceive);
System.out.println("retrieved : " + retrievedCount
+ " random bytes from entropy server: ");
for (int i = 0; i < retrievedCount; i++) {
System.out.println("random byte " + i + ": "
+ bytesToReceive[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pipe != null) {
try {
pipe.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* Create a request for Entropy Server
*
* @param byteCount
* @return
*/
private static byte[] createPipeRequest(int byteCount) {
byte[] commandBytes = new byte[8];
byte[] bytes = ByteBuffer.allocate(4).putInt(byteCount).array();
int d = 4;
for (int i = 3; i >= 0; i--) {
commandBytes[d++] = bytes[i];
}
return commandBytes;
}
}