vertx / io.vertx.ext.auth / VertxContextPRNG

VertxContextPRNG

interface VertxContextPRNG

A secure non blocking random number generator isolated to the current context. The PRNG is bound to the vert.x context and setup to close when the context shuts down.

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

Functions

current

open static fun current(): VertxContextPRNG

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 a java.lang.IllegalStateException is thrown.

open static fun current(context: Context): VertxContextPRNG

Get or create a secure non blocking random number generator using the provided vert.x context. This method will not throw an exception.

open static fun current(vertx: Vertx): VertxContextPRNG

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.

nextBytes

abstract fun nextBytes(bytes: ByteArray): Unit

Fills the given byte array with random bytes.

nextInt

abstract fun nextInt(): Int

Returns a secure random int

nextString

open fun nextString(length: Int): String

Returns a Base64 mime encoded String of random data with the given length. The length parameter refers to the length of the String before the encoding step.

Inheritors

PRNG

open class PRNG : VertxContextPRNG

Wrapper around secure random that periodically seeds the PRNG with new entropy. To avoid entropy exhaustion the entropy is only refreshed if the PRNG is used. This introduces a new variable which reduces the probability of cracking the random number generator.