SecureRandom in Java SE 10 and JDK 10

Specifically, the cryptographically strong kind of randomness that you can get with Java SE 10 and JDK 10 using SecureRandom.

To start: what is a SecureRandom? Well, it’s basically a fancy pants RNG (random number generator) that meets all sorts of crazy standards for being truly random. It’s not just some lame old dice roller or coin flipper this thing can generate numbers that are so unpredictable and chaotic that even the NSA would be impressed!

So how do you get your hands on one of these SecureRandoms? Easy peasy, my friend. Just call the constructor with no arguments:

// This script creates a new instance of the SecureRandom class, which is used to generate cryptographically secure random numbers.
SecureRandom random = new SecureRandom(); // "random" is a more appropriate variable name than "r1" as it reflects the purpose of the object.

// The SecureRandom class does not require any arguments to be passed in the constructor.
// This creates a default instance of the class with no specific settings.
// The generated numbers will be unpredictable and chaotic, making it suitable for security purposes.
SecureRandom random = new SecureRandom();

Or if you’re feeling fancy, you can use one of the `getInstance()` methods to specify a particular implementation or algorithm:

// This script creates two instances of the SecureRandom class and specifies different algorithms for each instance.

// The first instance uses the "NativePRNG" algorithm.
SecureRandom r2 = SecureRandom.getInstance("NativePRNG");

// The second instance uses the "DRBG" algorithm and specifies parameters for instantiation.
// The parameters include a key size of 128 bits, a reseed only flag, and null for additional parameters.
SecureRandom r3 = SecureRandom.getInstance("DRBG", DrbgParameters.instantiation(128, RESEED_ONLY, null));

Now that you’ve got your shiny new SecureRandom object, what can you do with it? Well, there are a bunch of methods for generating random numbers and seeds:

– `nextBytes()` generates an array of bytes containing cryptographically strong random values.
– `setSeed(byte[] seed)` sets the initial value used to generate future random numbers (useful if you want to ensure deterministic results).
– `reseed()` resets the internal state and generates a new set of random numbers based on the current entropy source.
– `getBytes()` returns an array containing the current seed value.

Some SecureRandom implementations accept additional parameters to further control their behavior:

// This script uses the SecureRandom class to generate random numbers and perform cryptographic operations.

// Create a new instance of SecureRandom using the SHA1PRNG algorithm
SecureRandom r = SecureRandom.getInstance("SHA1PRNG");

// Generate random bytes using SHA-1 as the entropy source
byte[] bytes = new byte[16];
r.nextBytes(bytes);

// Create a SecretKeySpec object using the given key and AES algorithm
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");

// Create a Mac object using the HmacSHA256 algorithm
Mac mac = Mac.getMac("HmacSHA256");

// Initialize the Mac object with the SecretKeySpec
mac.init(secretKeySpec);

// Convert the message to bytes
byte[] messageBytes = ...;

// Create a MessageDigest object using the SHA-256 algorithm
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

// Generate a hash of the message bytes
byte[] hashBytes = messageDigest.digest(messageBytes);

// Use the hash as a seed for deterministic DSA signature generation
r.setSeed(hashBytes);

// Update the Mac object with the hash bytes
mac.update(hashBytes);

// Generate the final Mac bytes
byte[] macBytes = mac.doFinal();

And that’s it! You now have access to some of the most secure random numbers in all of Java land. Just remember, these things can be slow and resource-intensive so use them wisely!

SICORPS