How do i do that? Can't seems to find a way. Securerandom doesn't seems to allow me to specify bit size anywhere
Using randomUUID() java. util. UUID is another Java class that can be used to generate a random string. It offers a static randomUUID() method that returns a random alphanumeric string of 32 characters.
Example 1: Java program to generate a random string Next, we have generated a random index number using the nextInt() method of the Random class. Using the random index number, we have generated the random character from the string alphabet. We then used the StringBuilder class to append all the characters together.
There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.
If your bit-count can be divded by 8, in other words, you need a full byte-count, you can use
Random random = ThreadLocalRandom.current();
byte[] r = new byte[256]; //Means 2048 bit
random.nextBytes(r);
String s = new String(r)
If you don't like the strange characters, encode the byte-array as base64:
For example, use the Apache Commons Codec and do:
Random random = ThreadLocalRandom.current();
byte[] r = new byte[256]; //Means 2048 bit
random.nextBytes(r);
String s = Base64.encodeBase64String(r);
Similar to the other answer with a minor detail
Random random = ThreadLocalRandom.current();
byte[] randomBytes = new byte[32];
random.nextBytes(randomBytes);
String encoded = Base64.getUrlEncoder().encodeToString(randomBytes)
Instead of simply using Base64 encoding, which can leave you with a '+' in the out, make sure it doesn't contain any characters which need to be further URL encoded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With