Android Guides | Samples

Java.Security.SecureRandom Class

This class generates cryptographically secure pseudo-random numbers.

See Also: SecureRandom

Syntax

[Android.Runtime.Register("java/security/SecureRandom", DoNotGenerateAcw=true)]
public class SecureRandom : Random

Remarks

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:

java Example

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.

Seeding SecureRandom may be insecure

A seed is an array of bytes used to bootstrap random number generation. To produce cryptographically secure random numbers, both the seed and the algorithm must be secure.

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 SecureRandom.SecureRandom(Byte[]) or calling SecureRandom.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.

[Android Documentation]

Requirements

Namespace: Java.Security
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Java.Security.SecureRandom are listed below.

See Also: Random

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.
A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Properties

[read-only]
AlgorithmString. Returns the name of the algorithm of this SecureRandom.
[read-only]
ProviderProvider. Returns the provider associated with this SecureRandom.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

GenerateSeed(Int32) : Byte[]
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this SecureRandom.
static
GetInstance(String) : SecureRandom
Returns a new instance of SecureRandom that utilizes the specified algorithm.
static
GetInstance(String, Provider) : SecureRandom
Returns a new instance of SecureRandom that utilizes the specified algorithm from the specified provider.
static
GetInstance(String, String) : SecureRandom
Returns a new instance of SecureRandom that utilizes the specified algorithm from the specified provider.
static
GetSeed(Int32) : Byte[]
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this SecureRandom.
SetSeed(Byte[])
Seeds this SecureRandom instance with the specified seed.

Protected Methods

override
Next(Int32) : Int32
Generates and returns an int containing the specified number of random bits (right justified, with leading zeros).