Interface VertxContextPRNG
- All Known Implementing Classes:
PRNG
When applicable, use of VertxContextPRNG rather than create new PRNG objects is helpful to keep the system entropy usage to the minimum avoiding potential blocking across the application.
The use of VertxContextPRNG is particularly appropriate when multiple handlers use random numbers.
- Author:
- Paulo Lopes
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()stop seeding the PRNGstatic VertxContextPRNGcurrent()Get or create a secure non blocking random number generator using the current vert.x context.static VertxContextPRNGGet or create a secure non blocking random number generator using the provided vert.x context.static VertxContextPRNGGet or create a secure non blocking random number generator using the current vert.x instance.booleanReturns a secure random booleanvoidnextBytes(byte[] bytes) Fills the given byte array with random bytes.doubleReturns a secure random double value.floatReturns a secure random float value.doubleReturns a secure random double value.intnextInt()Returns a secure random intintnextInt(int bound) Returns a secure random int, between 0 (inclusive) and the specified bound (exclusive).longnextLong()Returns a secure random longnextString(int length) Returns a Base64 url encoded String of random data with the given length.
-
Method Details
-
current
Get or create a secure non blocking random number generator using the current vert.x context. If there is no current context (i.e.: not running on the eventloop) then aIllegalStateExceptionis thrown.Note, if a context isn't allowed to be used, for example, exceptions are thrown on getting and putting data, the VertxContextPRNG falls back to instantiate a new instance of the PRNG per call.
- Returns:
- A secure non blocking random number generator.
- Throws:
IllegalStateException- when there is noContextinstance available.
-
current
Get or create a secure non blocking random number generator using the provided vert.x context. This method will not throw an exception.Note, if a context isn't allowed to be used, for example, exceptions are thrown on getting and putting data, the VertxContextPRNG falls back to instantiate a new instance of the PRNG per call.
- Parameters:
context- a Vert.x context.- Returns:
- A secure non blocking random number generator
- Throws:
IllegalStateException- when there is noVertxinstance available.
-
current
Get or create a secure non blocking random number generator using the current vert.x instance. Since the context might be different this method will attempt to use the current context first if available and then fall back to create a new instance of the PRNG.Note, if a context isn't allowed to be used, for example, exceptions are thrown on getting and putting data, the VertxContextPRNG falls back to instantiate a new instance of the PRNG per call.
- Parameters:
vertx- a Vert.x instance.- Returns:
- A secure non blocking random number generator.
-
close
void close()stop seeding the PRNG -
nextBytes
void nextBytes(byte[] bytes) Fills the given byte array with random bytes.- Parameters:
bytes- a byte array.
-
nextString
Returns a Base64 url encoded String of random data with the given length. The length parameter refers to the length of the String before the encoding step.- Parameters:
length- the desired string length before Base64 encoding.- Returns:
- A base 64 encoded string.
-
nextInt
int nextInt()Returns a secure random int- Returns:
- random int.
-
nextInt
int nextInt(int bound) Returns a secure random int, between 0 (inclusive) and the specified bound (exclusive).- Parameters:
bound- the upper bound (exclusive), which must be positive.- Returns:
- random int.
-
nextBoolean
boolean nextBoolean()Returns a secure random boolean- Returns:
- random boolean.
-
nextLong
long nextLong()Returns a secure random long- Returns:
- random long.
-
nextFloat
float nextFloat()Returns a secure random float value. The value is uniformly distributed between 0.0 and 1.0- Returns:
- random float.
-
nextDouble
double nextDouble()Returns a secure random double value. The value is uniformly distributed between 0.0 and 1.0- Returns:
- random double.
-
nextGaussian
double nextGaussian()Returns a secure random double value. The value is Gaussian ("normally") distributed with mean 0.0 and standard deviation 1.0- Returns:
- random double.
-