Table of Contents

Class SecureRandom

Namespace
Org.BouncyCastle.Security
Assembly
BouncyCastle.Cryptography.dll
public class SecureRandom : Random
Inheritance
SecureRandom
Derived
Inherited Members

Constructors

SecureRandom()

public SecureRandom()

SecureRandom(IRandomGenerator)

Use the specified instance of IRandomGenerator as random source.

public SecureRandom(IRandomGenerator generator)

Parameters

generator IRandomGenerator

The source to generate all random bytes from.

Remarks

This constructor performs no seeding of either the IRandomGenerator or the constructed SecureRandom. It is the responsibility of the client to provide proper seed material as necessary/appropriate for the given IRandomGenerator implementation.

SecureRandom(IRandomGenerator, int)

public SecureRandom(IRandomGenerator generator, int autoSeedLengthInBytes)

Parameters

generator IRandomGenerator
autoSeedLengthInBytes int

Fields

generator

protected readonly IRandomGenerator generator

Field Value

IRandomGenerator

Methods

GenerateSeed(int)

public virtual byte[] GenerateSeed(int length)

Parameters

length int

Returns

byte[]

GenerateSeed(Span<byte>)

public virtual void GenerateSeed(Span<byte> seed)

Parameters

seed Span<byte>

GetInstance(string)

Create and auto-seed an instance based on the given algorithm.

public static SecureRandom GetInstance(string algorithm)

Parameters

algorithm string

e.g. "SHA256PRNG"

Returns

SecureRandom

Remarks

Equivalent to GetInstance(algorithm, true)

GetInstance(string, bool)

Create an instance based on the given algorithm, with optional auto-seeding

public static SecureRandom GetInstance(string algorithm, bool autoSeed)

Parameters

algorithm string

e.g. "SHA256PRNG"

autoSeed bool

If true, the instance will be auto-seeded.

Returns

SecureRandom

GetNextBytes(SecureRandom, int)

public static byte[] GetNextBytes(SecureRandom secureRandom, int length)

Parameters

secureRandom SecureRandom
length int

Returns

byte[]

Next()

Returns a non-negative random integer.

public override int Next()

Returns

int

A 32-bit signed integer that is greater than or equal to 0 and less than MaxValue.

Next(int)

Returns a non-negative random integer that is less than the specified maximum.

public override int Next(int maxValue)

Parameters

maxValue int

The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to 0.

Returns

int

A 32-bit signed integer that is greater than or equal to 0, and less than maxValue; that is, the range of return values ordinarily includes 0 but not maxValue. However, if maxValue equals 0, maxValue is returned.

Exceptions

ArgumentOutOfRangeException

maxValue is less than 0.

Next(int, int)

Returns a random integer that is within a specified range.

public override int Next(int minValue, int maxValue)

Parameters

minValue int

The inclusive lower bound of the random number returned.

maxValue int

The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.

Returns

int

A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.

Exceptions

ArgumentOutOfRangeException

minValue is greater than maxValue.

NextBytes(byte[])

Fills the elements of a specified array of bytes with random numbers.

public override void NextBytes(byte[] buf)

Parameters

buf byte[]

Exceptions

ArgumentNullException

buffer is null.

NextBytes(byte[], int, int)

public virtual void NextBytes(byte[] buf, int off, int len)

Parameters

buf byte[]
off int
len int

NextBytes(Span<byte>)

Fills the elements of a specified span of bytes with random numbers.

public override void NextBytes(Span<byte> buffer)

Parameters

buffer Span<byte>

The array to be filled with random numbers.

NextDouble()

Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.

public override double NextDouble()

Returns

double

A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0.

NextInt()

public virtual int NextInt()

Returns

int

NextLong()

public virtual long NextLong()

Returns

long

SetSeed(byte[])

public virtual void SetSeed(byte[] seed)

Parameters

seed byte[]

SetSeed(long)

public virtual void SetSeed(long seed)

Parameters

seed long

SetSeed(Span<byte>)

public virtual void SetSeed(Span<byte> seed)

Parameters

seed Span<byte>