views:

81

answers:

2

I need to access SecureRandom Java Object from Javascript. My ultimate goal is to grab 4 bytes from PRNG and convert it to Javascript integer variable. According to http://download.oracle.com/javase/1.4.2/docs/api/java/security/SecureRandom.html, the following two lines of Java code are supposed to do grab 4 random bytes:

byte bytes[] = new byte[4];
random.nextBytes(bytes);

My problems is that I don't know how to 1) allocate byte array suitable for passing to Java method 2) parse that array into integer afterwards

So far I have managed to getSeed() method which returns an array of random bytes. When I render HTML code provided below in Firefox it shows "[B@16f70a4", which appears to be a pointer or something.

<script>
var sprng = new java.security.SecureRandom();
random = sprng.getSeed(4);
document.write(random + "<br/>\n");
</script>

This makes me think that I succeed to instantiate and access Java class, but have a problem with type conversion.

Can anyone please help me to write allocateJavaByteArray(N) and convertJavaByteArrayToInt(N) to let the following code work:

var sprng = new java.security.SecureRandom();
var nextBytes = allocateJavaByteArray(4);
srng.nextBytes(nextBytes);
var nextInt = convertJavaByteArrayToInt(4);

Thank you in advance.

A: 

Normally you'd generate the random number on the server and pass it in the Request to the jsp.

Tony Ennis
Tony, thank you for your comment, but JSP is not relevant to my situation. Everything happens on the client side.
abb
A: 

You could simply generate a random integer in the first place, like this:

var nextInt = sprng.nextInt();
andrewmu