Generate Cryptographically Random Data

suggest change

To generate samples of cryptographically random data:

final byte[] sample = new byte[16];

new SecureRandom().nextBytes(sample);

System.out.println("Sample: " + DatatypeConverter.printHexBinary(sample));

Produces output similar to:

Sample: E4F14CEA2384F70B706B53A6DF8C5EFE

Note that the call to nextBytes() may block while entropy is gathered depending on the algorithm being used.

To specify the algorithm and provider:

final byte[] sample = new byte[16];
final SecureRandom randomness = SecureRandom.getInstance("SHA1PRNG", "SUN");

randomness.nextBytes(sample);

System.out.println("Provider: " + randomness.getProvider());
System.out.println("Algorithm: " + randomness.getAlgorithm());
System.out.println("Sample: " + DatatypeConverter.printHexBinary(sample));

Produces output similar to:

Provider: SUN version 1.8
Algorithm: SHA1PRNG
Sample: C80C44BAEB352FD29FBBE20489E4C0B9

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Security and Cryptograhy:
* Generate Cryptographically Random Data

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
136 Security and Cryptograhy
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap