Top Description Fields Constructors Methods
java.security

public abstract Class SecureRandomSpi

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
sun.security.provider.DRBG, sun.security.provider.SecureRandom, sun.security.provider.NativePRNG, sun.security.provider.NativePRNG.Blocking, sun.security.provider.NativePRNG.NonBlocking

This class defines the Service Provider Interface (SPI) for the SecureRandom class.

All the abstract methods in this class must be implemented by each service provider who wishes to supply the implementation of a cryptographically strong pseudo-random number generator.

Implementation Specification

If the SecureRandomSpi(SecureRandomParameters) constructor is overridden in an implementation, it will always be called whenever a SecureRandom is instantiated. Precisely, if an object is instantiated with one of SecureRandom's getInstance methods without a SecureRandomParameters parameter, the constructor will be called with a null argument and the implementation is responsible for creating its own SecureRandomParameters parameter for use when engineGetParameters() is called. If an object is instantiated with one of SecureRandom's getInstance methods with a SecureRandomParameters argument, the constructor will be called with that argument. The engineGetParameters() method must not return null.

Otherwise, if the SecureRandomSpi(SecureRandomParameters) constructor is not overridden in an implementation, the SecureRandomSpi() constructor must be overridden, and it will be called if an object is instantiated with one of SecureRandom's getInstance methods without a SecureRandomParameters argument. Calling one of SecureRandom's getInstance methods with a SecureRandomParameters argument will never return an instance of this implementation. The engineGetParameters() method must return null.

See SecureRandom for additional details on thread safety. By default, a SecureRandomSpi implementation is considered to be not safe for use by multiple concurrent threads and SecureRandom will synchronize access to each of the applicable engine methods (see SecureRandom for the list of methods). However, if a SecureRandomSpi implementation is thread-safe, the service provider attribute "ThreadSafe" should be set to "true" during its registration, as follows:

put("SecureRandom.AlgName ThreadSafe", "true");
or
putService(new Service(this, "SecureRandom", "AlgName", className,
         null, Map.of("ThreadSafe", "true")));
SecureRandom will call the applicable engine methods without any synchronization.
Since
1.2

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public
SecureRandomSpi()

Constructor without a parameter.

protected
SecureRandomSpi(SecureRandomParameters
the SecureRandomParameters object. This argument can be null.
params
)

Constructor with a parameter.

Method Summary

Modifier and TypeMethod and Description
protected abstract byte[]

Returns:

the seed bytes.
engineGenerateSeed
(int
the number of seed bytes to generate.
numBytes
)

Returns the given number of seed bytes.

protected SecureRandomParameters

Returns:

the effective SecureRandomParameters parameters, or null if no parameters were used.
engineGetParameters
()

Returns the effective SecureRandomParameters for this SecureRandom instance.

protected abstract void
engineNextBytes(byte[]
the array to be filled in with random bytes.
bytes
)

Generates a user-specified number of random bytes.

protected void
engineNextBytes(byte[]
the array to be filled in with random bytes
bytes
,
SecureRandomParameters
additional parameters
params
)

Generates a user-specified number of random bytes with additional parameters.

protected void
engineReseed(SecureRandomParameters
extra parameters, can be null.
params
)

Reseeds this random object with entropy input read from its entropy source with additional parameters.

protected abstract void
engineSetSeed(byte[]
the seed.
seed
)

Reseeds this random object with the given seed.

public String

Returns:

the string representation
toString
()

Overrides java.lang.Object.toString.

Returns a Human-readable string representation of this SecureRandom.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@Serial

Constructor Detail

SecureRandomSpiback to summary
public SecureRandomSpi()

Constructor without a parameter.

SecureRandomSpiback to summary
protected SecureRandomSpi(SecureRandomParameters params)

Constructor with a parameter.

Parameters
params:SecureRandomParameters

the SecureRandomParameters object. This argument can be null.

Exceptions
IllegalArgumentException:
if params is unrecognizable or unsupported by this SecureRandom
Since
9

Method Detail

engineGenerateSeedback to summary
protected abstract byte[] engineGenerateSeed(int numBytes)

Returns the given number of seed bytes. This call may be used to seed other random number generators.

Parameters
numBytes:int

the number of seed bytes to generate.

Returns:byte[]

the seed bytes.

engineGetParametersback to summary
protected SecureRandomParameters engineGetParameters()

Returns the effective SecureRandomParameters for this SecureRandom instance.

Implementation Specification

The default implementation returns null.

Returns:SecureRandomParameters

the effective SecureRandomParameters parameters, or null if no parameters were used.

Since
9
engineNextBytesback to summary
protected abstract void engineNextBytes(byte[] bytes)

Generates a user-specified number of random bytes.

Some random number generators can only generate a limited amount of random bytes per invocation. If the size of bytes is greater than this limit, the implementation should invoke its generation process multiple times to completely fill the buffer before returning from this method.

Parameters
bytes:byte[]

the array to be filled in with random bytes.

engineNextBytesback to summary
protected void engineNextBytes(byte[] bytes, SecureRandomParameters params)

Generates a user-specified number of random bytes with additional parameters.

Some random number generators can only generate a limited amount of random bytes per invocation. If the size of bytes is greater than this limit, the implementation should invoke its generation process multiple times to completely fill the buffer before returning from this method.

Implementation Specification

The default implementation throws an UnsupportedOperationException.

Parameters
bytes:byte[]

the array to be filled in with random bytes

params:SecureRandomParameters

additional parameters

Exceptions
UnsupportedOperationException:
if the implementation has not overridden this method
IllegalArgumentException:
if params is null, illegal or unsupported by this SecureRandom
Since
9
engineReseedback to summary
protected void engineReseed(SecureRandomParameters params)

Reseeds this random object with entropy input read from its entropy source with additional parameters.

If this method is called by SecureRandom#reseed(), params will be null.

Do not override this method if the implementation does not support reseeding.

Implementation Specification

The default implementation throws an UnsupportedOperationException.

Parameters
params:SecureRandomParameters

extra parameters, can be null.

Exceptions
UnsupportedOperationException:
if the implementation has not overridden this method
IllegalArgumentException:
if params is illegal or unsupported by this SecureRandom
Since
9
engineSetSeedback to summary
protected abstract void engineSetSeed(byte[] seed)

Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.

Parameters
seed:byte[]

the seed.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a Human-readable string representation of this SecureRandom.

Returns:String

the string representation

Annotations
@Override