There
are many ways to generate random bytes in Java.
a. Using nextBytes() method of Random
class
b. Using nextBytes method of
SecureRandom class
c. Using
SecureRandom.getInstanceStrong().nextBytes(bytes)
d. Using
ThreadLocalRandom.current().nextBytes(bytes)
Using nextBytes()
method of Random class
Random
class provides nextBytes() method, it takes a byte array as argument and fill
the input byte array with random bytes.
Random random = new Random();
byte[] b = new byte[20];
random.nextBytes(b);
RandomDemo.java
package com.sample.Random; import java.util.Random; public class RandomDemo { private static void printArray(byte[] byteArr) { for (byte b : byteArr) { System.out.print(b + ","); } } public static void main(String args[]) { Random random = new Random(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); } }
Sample Output
-56,108,-21,-73,83,1,-2,-117,-52,-66,-96,-51,89,-79,100,-106,121,68,79,81,
Using nextBytes
method of SecureRandom class
'java.security.SecureRandom'
class provides nextBytes method, it takes byte array as argument and fill the
byte array with random bytes. I would prefer to use Secure Random to generate
random numbers than Random. It is because, Random class uses System clock to
generate the seed, where as SecureRandom class uses random data from operating
system (For ex interval betweem keystorkes, cpul cycles etc.,) to generate the
seed. SecureRandom is more secure as compared to Random.
SecureRandom random = new
SecureRandom();
byte[] b = new byte[20];
random.nextBytes(b);
RandomDemo.java
package com.sample.Random; import java.security.SecureRandom; public class RandomDemo { private static void printArray(byte[] byteArr) { for (byte b : byteArr) { System.out.print(b + ","); } } public static void main(String args[]) { SecureRandom random = new SecureRandom(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); } }
Sample Output
9,62,-83,114,6,70,68,-63,-94,33,-100,-75,-91,-36,-60,-26,27,-116,64,-113,
Using
SecureRandom.getInstanceStrong().nextBytes(bytes)
SecureRandom.getInstanceStrong()
is avialble in Java1.8. 'SecureRandom.getInstanceStrong()' returns a secure
random object that was selected by using the algorithms/providers specified in
the securerandom.strongAlgorithms (Ex: Windows-PRNG:SunMSCAPI,SHA1PRNG:SUN)
Get the algorithms
specified in 'securerandom.strongAlgorithms'
By
using below statements, you can get the algorithms specified in
'securerandom.strongAlgorithms' property
String randomAlgorithms =
Security.getProperty("securerandom.strongAlgorithms");
System.out.println(randomAlgorithms);
Below
statements are used to generate random bytes.
SecureRandom random =
SecureRandom.getInstanceStrong();
byte[] b = new byte[20];
random.nextBytes(b);
RandomDemo.java
package com.sample.Random; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class RandomDemo { private static void printArray(byte[] byteArr) { for (byte b : byteArr) { System.out.print(b + ","); } } public static void main(String args[]) throws NoSuchAlgorithmException { SecureRandom random = SecureRandom.getInstanceStrong(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); } }
Sample Output
73,-22,-8,-24,-80,-50,113,-33,-34,-69,104,-66,-74,-114,-104,-118,102,-26,-106,-72,
Prefer
to use 'SecureRandom.getInstanceStrong()' over all other methods
Using nextBytes
method of ThreadLocalRandom class
Below
statements are used to generate random numbers.
ThreadLocalRandom
threadLocalRandom = ThreadLocalRandom.current();
byte b[] = new byte[20];
threadLocalRandom.nextBytes(b);
RandomDemo.java
package com.sample.Random; import java.security.NoSuchAlgorithmException; import java.util.concurrent.ThreadLocalRandom; public class RandomDemo { private static void printArray(byte[] byteArr) { for (byte b : byteArr) { System.out.print(b + ","); } } public static void main(String args[]) throws NoSuchAlgorithmException { ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); byte b[] = new byte[20]; threadLocalRandom.nextBytes(b); printArray(b); } }
Sample Output
-14,5,0,-105,37,65,95,-45,112,-79,72,-59,68,42,36,2,25,-70,13,-99,
You may like
No comments:
Post a Comment