SecureRandom Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
This class provides a cryptographically strong random number generator (RNG).
[Android.Runtime.Register("java/security/SecureRandom", DoNotGenerateAcw=true)]
public class SecureRandom : Java.Util.Random
[<Android.Runtime.Register("java/security/SecureRandom", DoNotGenerateAcw=true)>]
type SecureRandom = class
inherit Random
- Inheritance
- Attributes
Remarks
This class provides a cryptographically strong random number generator (RNG).
A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Moduleshttp://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.140-2.pdf, section 4.9.1. Additionally, SecureRandom
must produce non-deterministic output. Therefore any seed material passed to a SecureRandom
object must be unpredictable, and all SecureRandom
output sequences must be cryptographically strong, as described in RFC 4086: Randomness Requirements for Securityhttp://tools.ietf.org/html/rfc4086.
Many SecureRandom
implementations are in the form of a pseudo-random number generator (PRNG, also known as deterministic random bits generator or DRBG), which means they use a deterministic algorithm to produce a pseudo-random sequence from a random seed. Other implementations may produce true random numbers, and yet others may use a combination of both techniques.
A caller obtains a SecureRandom
instance via the no-argument constructor or one of the getInstance
methods. For example:
<blockquote>
SecureRandom r1 = new SecureRandom();
SecureRandom r2 = SecureRandom.getInstance("NativePRNG");
SecureRandom r3 = SecureRandom.getInstance("DRBG",
DrbgParameters.instantiation(128, RESEED_ONLY, null));
</blockquote>
The third statement above returns a SecureRandom
object of the specific algorithm supporting the specific instantiate parameters. The implementation's effective instantiated parameters must match this minimum request but is not necessarily the same. For example, even if the request does not require a certain feature, the actual instantiation can provide the feature. An implementation may lazily instantiate a SecureRandom
until it's actually used, but the effective instantiate parameters must be determined right after it's created and #getParameters()
should always return the same result unchanged.
Typical callers of SecureRandom
invoke the following methods to retrieve random bytes:
<blockquote>
SecureRandom random = new SecureRandom();
byte[] bytes = new byte[20];
random.nextBytes(bytes);
</blockquote>
Callers may also invoke the #generateSeed
method to generate a given number of seed bytes (to seed other random number generators, for example):
<blockquote>
byte[] seed = random.generateSeed(20);
</blockquote>
A newly created PRNG SecureRandom
object is not seeded (except if it is created by #SecureRandom(byte[])
). The first call to nextBytes
will force it to seed itself from an implementation- specific entropy source. This self-seeding will not occur if setSeed
was previously called.
A SecureRandom
can be reseeded at any time by calling the reseed
or setSeed
method. The reseed
method reads entropy input from its entropy source to reseed itself. The setSeed
method requires the caller to provide the seed.
Please note that reseed
may not be supported by all SecureRandom
implementations.
Some SecureRandom
implementations may accept a SecureRandomParameters
parameter in its #nextBytes(byte[], SecureRandomParameters)
and #reseed(SecureRandomParameters)
methods to further control the behavior of the methods.
Note: Depending on the implementation, the generateSeed
, reseed
and nextBytes
methods may block as entropy is being gathered, for example, if the entropy source is /dev/random on various Unix-like operating systems.
<h2> Thread safety </h2> SecureRandom
objects are safe for use by multiple concurrent threads.
Added in 1.1.
Java documentation for java.security.SecureRandom
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
SecureRandom() |
Constructs a secure random number generator (RNG) implementing the default random number algorithm. |
SecureRandom(Byte[]) |
Constructs a secure random number generator (RNG) implementing the default random number algorithm. |
SecureRandom(IntPtr, JniHandleOwnership) |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
SecureRandom(SecureRandomSpi, Provider) |
Creates a |
Properties
Algorithm |
Returns the name of the algorithm implemented by this
|
Class |
Returns the runtime class of this |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
InstanceStrong |
Returns a |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
Parameters |
Returns the effective |
PeerReference | (Inherited from Object) |
Provider |
Returns the provider of this |
ThresholdClass |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
ThresholdType |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Methods
Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
GenerateSeed(Int32) |
Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. |
GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
GetInstance(String, ISecureRandomParameters, Provider) |
Returns a |
GetInstance(String, ISecureRandomParameters, String) |
Returns a |
GetInstance(String, ISecureRandomParameters) |
Returns a |
GetInstance(String, Provider) |
Returns a |
GetInstance(String, String) |
Returns a |
GetInstance(String) |
Returns a |
GetSeed(Int32) |
Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. |
JavaFinalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
Next(Int32) |
Generates an integer containing the user-specified number of pseudo-random bits (right justified, with leading zeros). |
NextBoolean() |
Returns the next pseudorandom, uniformly distributed
|
NextBytes(Byte[], ISecureRandomParameters) |
Generates a user-specified number of random bytes with additional parameters. |
NextBytes(Byte[]) |
Generates random bytes and places them into a user-supplied byte array. (Inherited from Random) |
NextDouble() |
Returns the next pseudorandom, uniformly distributed
|
NextFloat() |
Returns the next pseudorandom, uniformly distributed |
NextGaussian() |
Returns the next pseudorandom, Gaussian ("normally") distributed
|
NextInt() |
Returns the next pseudorandom, uniformly distributed |
NextInt(Int32) |
Returns a pseudorandom, uniformly distributed |
NextLong() |
Returns the next pseudorandom, uniformly distributed |
Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
Reseed() |
Reseeds this |
Reseed(ISecureRandomParameters) |
Reseeds this |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
SetSeed(Byte[]) |
Reseeds this random object with the given seed. |
SetSeed(Int64) |
Sets the seed of this random number generator using a single
|
ToArray<T>() | (Inherited from Object) |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
UnregisterFromRuntime() | (Inherited from Object) |
Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
IJavaPeerable.Disposed() | (Inherited from Object) |
IJavaPeerable.DisposeUnlessReferenced() | (Inherited from Object) |
IJavaPeerable.Finalized() | (Inherited from Object) |
IJavaPeerable.JniManagedPeerState | (Inherited from Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) | (Inherited from Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) | (Inherited from Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) | (Inherited from Object) |
Extension Methods
JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
JavaCast<TResult>(IJavaObject) | |
GetJniTypeName(IJavaPeerable) |
Gets the JNI name of the type of the instance |
JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |