Link to home
Start Free TrialLog in
Avatar of gremwell
gremwell

asked on

Passing byte arrays between Java and JavaScript

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>

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

check this Math.random() method
http://www.w3schools.com/jsref/jsref_random.asp

It returns a value between 0 and 1, so you can multiple that value with 9999 to get a 4 digit random number
Avatar of gremwell
gremwell

ASKER

I'm aware of Math.random(), but it uses a timestamp in milliseconds to seed itself. It is not secure enough for my purposes. I want to use Java's SecureRandom, which uses OS-specific sources of entropy to seed itself.
If you want to use Java's SecureRandom, then you have to send it as a response of servlet invocation.
What servlets have to do with the OP? It is possible to instantiate Java classes from Javascript and call its methods. The example I have provided in the OP proves it. If you believe I am mistaken, please justify your point of view.
Okay.
Can you post the code that you have tried which is printing "[B@16f70a4"? since it looks like that this looks like an Sysout of an object rather than a primitive value. I know you have already posted a code above but there are 2 such codes i can see in your method.

<html>
<body>
<script>

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

</script>
</body>
</html>

Open in new window

what does this print?

<html>
<body>
<script>

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

</script>
</body>
</html>

Above is an example of converting byte array to string
I guess you need to convert the byte array to an int check this
http://www.java2s.com/Code/Java/Collections-Data-Structure/ConvertbytearraytoIntegerandLong.htm

Yes, what you are getting is a byte[] object.

If you need an int, you convert the binary content of the array to an integer by doing simple math (multiply each '1' by two power n, where n is the position of the bit, and add the results) or using the code at this page: http://www.petefreitag.com/item/183.cfm

You can port the above function to JS or write your own.

> what does this print?
something like "[B@16f70a4"

> I guess you need to convert the byte array to an int check this
This is not relevant. I am writing in Javascript not in Java.
> You can port the above function to JS or write your own. My knowledge of JS is not enough to do that. In the OP I have asked for help to finish the implementation of the following function, can you please help with it? See the code below, the problemmatic parts are marked with XXX.
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;
}

Open in new window

I am not good at JS either but looking at this page https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators it appears that JS is capable of doing bitwise operations, hence porting the Java code mentioned in my previous post or writing your own should be trivial.

This bit is a surprise to me:

var sprng = new java.security.SecureRandom();

I did not know you can invoke Java code directly from JavaScript !
> hence porting the Java code mentioned in my previous post or writing your own should be trivial
Does not look trivial to me. Can you do it please?
try the following
<html>
<body>
<%
byte[] random= new byte[4];
SecureRandom sprng = new java.security.SecureRandom();
random = sprng.getSeed(4);
%>
<script>
var JS_random = new Array(4);
<%
for(int i= 0; i<random.length;i++){
 %> JS_random[<%=i%>] = '<%=random[i]%>';
<%
}
%>
document.write(JS_random + "<br/>\n");
</script>
</body>
</html>

Open in new window

Thank you, but I don't need JSP. Everything should happen on the client side.

Yes, I insist that it is possible to invoke Java's methods from Javascript. Please try the examples I gave in the OP in a Mozilla-based browser (tested in Firefox 3.0) and/or see http://docstore.mik.ua/orelly/webprog/jscript/ch22_03.htm .
SOLUTION
Avatar of magedroshdy
magedroshdy
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
good to hear you found what you're searching for