Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RNGCryptoServiceProvider in Java

Tags:

java

c#

random

I'm converting some C# code to Java. I can't find an equivalent to RNGCryptoServiceProvider. How do I do this?

private static String GetRandomSalt()
{
    RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
    byte[] salt = new byte[32]; //256 bits
    random.GetBytes(salt);
    ...
}
like image 418
Al Lelopath Avatar asked Dec 14 '16 18:12

Al Lelopath


1 Answers

To expand on my comment:

Java's SecureRandom is the equivalent you're looking for.

SecureRandom random = new SecureRandom();
byte[] salt = new byte[32];
random.nextBytes(salt);

The documentation details some other ways to get an instance of SecureRandom, depending on your requirements.

like image 187
Sam Avatar answered Oct 17 '22 03:10

Sam