| java.lang.Object | ||
| ↳ | java.util.Random | |
| ↳ | java.security.SecureRandom | |
This class generates cryptographically secure pseudo-random numbers.
It is best to invoke
SecureRandom
using the default constructor.
This will provide an instance of the most cryptographically strong
provider available:
SecureRandom sr = new SecureRandom();
byte[] output = new byte[16];
sr.nextBytes(output);
The default algorithm is defined by the first
SecureRandomSpi
provider found in the installed security providers. Use
Security
to install custom
SecureRandomSpi
providers.
Note that the output of a
SecureRandom
instance should never
be relied upon to be deterministic. For deterministic output from a given
input, see
MessageDigest
which provides one-way hash functions.
For deriving keys from passwords, see
SecretKeyFactory
.
SecureRandom
may be
insecure
By default, instances of this class will generate an initial seed using
an internal entropy source, such as
/dev/urandom
. This seed is
unpredictable and appropriate for secure use.
Using the
seeded constructor
or calling
setSeed(byte[])
may completely replace the cryptographically strong
default seed causing the instance to return a predictable sequence of
numbers unfit for secure use. Due to variations between implementations
it is not recommended to use
setSeed
at all.
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Constructs a new
SecureRandom
that uses the default algorithm.
|
||||||||||
|
|
Constructs a new seeded
SecureRandom
that uses the default
algorithm.
|
||||||||||
| Protected Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Constructs a new instance of
SecureRandom
using the given
implementation from the specified provider.
|
||||||||||
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Generates and returns the specified number of seed bytes, computed using
the seed generation algorithm used by this
SecureRandom
.
|
||||||||||
|
|
Returns the name of the algorithm of this
SecureRandom
.
|
||||||||||
|
|
Returns a new instance of
SecureRandom
that utilizes the
specified algorithm from the specified provider.
|
||||||||||
|
|
Returns a new instance of
SecureRandom
that utilizes the
specified algorithm from the specified provider.
|
||||||||||
|
|
Returns a new instance of
SecureRandom
that utilizes the
specified algorithm.
|
||||||||||
|
|
Returns the provider associated with this
SecureRandom
.
|
||||||||||
|
|
Generates and returns the specified number of seed bytes, computed using
the seed generation algorithm used by this
SecureRandom
.
|
||||||||||
|
|
Generates and stores random bytes in the given
byte[]
for each
array element.
|
||||||||||
|
|
Seeds this
SecureRandom
instance with the specified
seed
.
|
||||||||||
|
|
Seeds this
SecureRandom
instance with the specified eight-byte
seed
.
|
||||||||||
| Protected Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Generates and returns an
int
containing the specified number of
random bits (right justified, with leading zeros).
|
||||||||||
|
[Expand]
Inherited Methods
|
|||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.util.Random
|
|||||||||||
From class
java.lang.Object
|
|||||||||||
Constructs a new
SecureRandom
that uses the default algorithm.
Constructs a new seeded
SecureRandom
that uses the default
algorithm.
Seeding
SecureRandom
may be
insecure
.
Constructs a new instance of
SecureRandom
using the given
implementation from the specified provider.
| secureRandomSpi | the implementation. |
|---|---|
| provider | the security provider. |
Generates and returns the specified number of seed bytes, computed using
the seed generation algorithm used by this
SecureRandom
.
| numBytes | the number of seed bytes. |
|---|
Returns the name of the algorithm of this
SecureRandom
.
SecureRandom
.
Returns a new instance of
SecureRandom
that utilizes the
specified algorithm from the specified provider.
| algorithm | the name of the algorithm to use. |
|---|---|
| provider | the name of the provider. |
SecureRandom
that utilizes the
specified algorithm from the specified provider.
| NoSuchAlgorithmException | if the specified algorithm is not available. |
|---|---|
| NoSuchProviderException | if the specified provider is not available. |
| NullPointerException |
if
algorithm
is
null
.
|
| IllegalArgumentException |
if
provider == null || provider.isEmpty()
|
Returns a new instance of
SecureRandom
that utilizes the
specified algorithm from the specified provider.
| algorithm | the name of the algorithm to use. |
|---|---|
| provider | the security provider. |
SecureRandom
that utilizes the
specified algorithm from the specified provider.
| NoSuchAlgorithmException | if the specified algorithm is not available. |
|---|---|
| NullPointerException |
if
algorithm
is
null
.
|
| IllegalArgumentException |
if
provider == null
|
Returns a new instance of
SecureRandom
that utilizes the
specified algorithm.
| algorithm | the name of the algorithm to use. |
|---|
SecureRandom
that utilizes the
specified algorithm.
| NoSuchAlgorithmException | if the specified algorithm is not available. |
|---|---|
| NullPointerException |
if
algorithm
is
null
.
|
Returns the provider associated with this
SecureRandom
.
SecureRandom
.
Generates and returns the specified number of seed bytes, computed using
the seed generation algorithm used by this
SecureRandom
.
| numBytes | the number of seed bytes. |
|---|
Generates and stores random bytes in the given
byte[]
for each
array element.
| bytes |
the
byte[]
to be filled with random bytes.
|
|---|
Seeds this
SecureRandom
instance with the specified
seed
.
Seeding
SecureRandom
may be
insecure
.
Seeds this
SecureRandom
instance with the specified eight-byte
seed
.
Seeding
SecureRandom
may
be insecure
.
Generates and returns an
int
containing the specified number of
random bits (right justified, with leading zeros).
| numBits | number of bits to be generated. An input value should be in the range [0, 32]. |
|---|
int
containing the specified number of random bits.