Compartilhar via


SecureRandom Class

Definition

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
SecureRandom
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://csrc.nist.gov/cryptval/140-2.htm, 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 1750: Randomness Recommendations for Securityhttp://www.ietf.org/rfc/rfc1750.txt.

A caller obtains a SecureRandom instance via the no-argument constructor or one of the getInstance methods:

SecureRandom random = new SecureRandom();

Many SecureRandom implementations are in the form of a pseudo-random number generator (PRNG), which means they use a deterministic algorithm to produce a pseudo-random sequence from a true random seed. Other implementations may produce true random numbers, and yet others may use a combination of both techniques.

Typical callers of SecureRandom invoke the following methods to retrieve random bytes:

SecureRandom random = new SecureRandom();
                 byte bytes[] = new byte[20];
                 random.nextBytes(bytes);

Callers may also invoke the generateSeed method to generate a given number of seed bytes (to seed other random number generators, for example):

byte seed[] = random.generateSeed(20);

Note: Depending on the implementation, the generateSeed and nextBytes methods may block as entropy is being gathered, for example, if they need to read from /dev/random on various Unix-like operating systems.

The SHA1PRNG algorithm from the Crypto provider has been deprecated as it was insecure, and also incorrectly used by some apps as a key derivation function. See Security "Crypto" provider deprecated in Android N for details.

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 SecureRandom object.

Properties

Algorithm

Returns the name of the algorithm implemented by this SecureRandom object.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
InstanceStrong

Returns a SecureRandom object.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
Provider

Returns the provider of this SecureRandom object.

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, Provider)

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.

GetInstance(String, String)

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.

GetInstance(String)

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm.

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 boolean value from this random number generator's sequence.

(Inherited from Random)
NextBytes(Byte[])

Generates random bytes and places them into a user-supplied byte array.

(Inherited from Random)
NextDouble()

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

(Inherited from Random)
NextFloat()

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

(Inherited from Random)
NextGaussian()

Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

(Inherited from Random)
NextInt()

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

(Inherited from Random)
NextInt(Int32)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

(Inherited from Random)
NextLong()

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

(Inherited from Random)
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)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetSeed(Byte[])

Reseeds this random object.

SetSeed(Int64)

Sets the seed of this random number generator using a single long seed.

(Inherited from Random)
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)

Applies to