troubleshooting Question

Passing byte arrays between Java and JavaScript

Avatar of gremwell
gremwell asked on
JavaJavaScript
17 Comments1 Solution2725 ViewsLast Modified:
I need to access SecureRandom (http://download.oracle.com/javase/1.4.2/docs/api/java/security/SecureRandom.html) Java Object from Javascript. My ultimate goal is to grab 4 random bytes from PRNG and convert it to Javascript integer variable.

In Java example the following two lines of code are supposed to do grab 4 random bytes:
      byte bytes[] = new byte[4];
      random.nextBytes(bytes);
How do I write the same in Javascript?

PS. The attached code appears to be able to access getSeed() function which returns an array of random bytes. When I run it, my browser (Firefox) displays "[B@16f70a4", which appears to be a pointer or something. This makes me think that I succeed to instantiate and access Java class, but have a problem with type conversion.

I appreciate your help to finish the implementaiton of the following function:

var sprng = new java.security.SecureRandom();
function getNextSrngInt() {
   var sprng = new java.security.SecureRandom();
   var nextBytes = XXX 4 bytes array suitable for Java XXX
   srng.nextBytes(nextBytes);
   XXX convert nextBytes to nextInt XXX
   return nextInt;
}

<html>
<body>
<script>

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

</script>
</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 17 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros