Class SecureRandom
java.lang.Object
com.codename1.security.SecureRandom
Cryptographically secure random number generator. Delegates to the
platform's native CSPRNG (/dev/urandom on Unix-style systems, the
Windows BCryptGenRandom on Windows, SecRandomCopyBytes on iOS, the
hardware RNG on devices that expose one).
Use this class -- not java.util.Random or Math.random() -- whenever the
output is going to be used as a key, nonce, salt, session token, password
reset code or any other security-sensitive value.
Example
byte[] iv = SecureRandom.bytes(12); // AES-GCM nonce
int code = SecureRandom.intBelow(1_000_000); // 6-digit code
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]bytes(int length) Returns a freshlength-byte array filled with secure random bytes.static voidfill(byte[] out) Fillsoutwith secure random bytes.static intintBelow(int bound) Returns a uniformly distributed random int in[0, bound).static longlongBelow(long bound) Returns a uniformly distributed random long in[0, bound).
-
Method Details
-
bytes
public static byte[] bytes(int length) Returns a freshlength-byte array filled with secure random bytes. -
fill
public static void fill(byte[] out) Fillsoutwith secure random bytes. -
intBelow
public static int intBelow(int bound) Returns a uniformly distributed random int in[0, bound).boundmust be positive. -
longBelow
public static long longBelow(long bound) Returns a uniformly distributed random long in[0, bound).boundmust be positive.
-