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
generatorIRandomGeneratorThe 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
generatorIRandomGeneratorautoSeedLengthInBytesint
Fields
generator
protected readonly IRandomGenerator generator
Field Value
Methods
GenerateSeed(int)
public virtual byte[] GenerateSeed(int length)
Parameters
lengthint
Returns
- byte[]
GenerateSeed(Span<byte>)
public virtual void GenerateSeed(Span<byte> seed)
Parameters
GetInstance(string)
Create and auto-seed an instance based on the given algorithm.
public static SecureRandom GetInstance(string algorithm)
Parameters
algorithmstringe.g. "SHA256PRNG"
Returns
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
Returns
GetNextBytes(SecureRandom, int)
public static byte[] GetNextBytes(SecureRandom secureRandom, int length)
Parameters
secureRandomSecureRandomlengthint
Returns
- byte[]
Next()
Returns a non-negative random integer.
public override int Next()
Returns
Next(int)
Returns a non-negative random integer that is less than the specified maximum.
public override int Next(int maxValue)
Parameters
maxValueintThe exclusive upper bound of the random number to be generated.
maxValuemust 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 notmaxValue. However, ifmaxValueequals 0,maxValueis returned.
Exceptions
- ArgumentOutOfRangeException
maxValueis 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
minValueintThe inclusive lower bound of the random number returned.
maxValueintThe exclusive upper bound of the random number returned.
maxValuemust be greater than or equal tominValue.
Returns
- int
A 32-bit signed integer greater than or equal to
minValueand less thanmaxValue; that is, the range of return values includesminValuebut notmaxValue. IfminValueequalsmaxValue,minValueis returned.
Exceptions
- ArgumentOutOfRangeException
minValueis greater thanmaxValue.
NextBytes(byte[])
Fills the elements of a specified array of bytes with random numbers.
public override void NextBytes(byte[] buf)
Parameters
bufbyte[]
Exceptions
- ArgumentNullException
bufferis null.
NextBytes(byte[], int, int)
public virtual void NextBytes(byte[] buf, int off, int len)
Parameters
NextBytes(Span<byte>)
Fills the elements of a specified span of bytes with random numbers.
public override void NextBytes(Span<byte> buffer)
Parameters
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
NextLong()
public virtual long NextLong()
Returns
SetSeed(byte[])
public virtual void SetSeed(byte[] seed)
Parameters
seedbyte[]
SetSeed(long)
public virtual void SetSeed(long seed)
Parameters
seedlong
SetSeed(Span<byte>)
public virtual void SetSeed(Span<byte> seed)