Cha Cha Class
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
public sealed class ChaCha : CounterBasedRandomSource<CounterBasedGenerator32<ChaChaEngine>, uint>,
IRandomSourceFactory<ChaCha>, IJumpable, IAdvanceable- Inheritance
- Object → RandomSource<CounterBasedGenerator32<ChaChaEngine>> → CounterBasedRandomSource<CounterBasedGenerator32<ChaChaEngine>, UInt32> → ChaCha
- Implements
- IAdvanceable, IJumpable, IRandomSourceFactory<ChaCha>
Remarks
Use the ChaCha class to represent a cryptographically secure pseudo-random number generator that uses the ChaCha stream cipher with 20 rounds by default.
ChaCha20 provides excellent security properties suitable for cryptographic applications. It is based on the ChaCha cipher designed by Daniel J. Bernstein. The 20-round variant is the standard version used in TLS 1.3, SSH, and other security-critical protocols.
This generator is suitable for applications requiring cryptographic-quality randomness, such as key generation, secure token creation, and cryptographic protocols.
ChaCha has been extensively analyzed and is particularly efficient on modern CPUs. This implementation supports 8, 12, and 20 round variants, with 20 rounds being the default.
Variant support:
Use WithChaChaVariant(RandomOptions, ChaChaVariant) or WithChaChaRounds(RandomOptions, Int32) to configure the variant.
Reference: Daniel J. Bernstein. "ChaCha, a variant of Salsa20." 2008. RFC 8439: "ChaCha20 and Poly1305 for IETF Protocols."
Example
var rng = new ChaCha20(54321);
byte[] key = new byte[32];
rng.NextBytes(key);Constructors
| Cha | Initializes a new instance of the ChaCha class using OS entropy. |
| Cha | Initializes a new instance of the ChaCha class with the specified options. |
| Cha | Initializes a new instance of the ChaCha class from saved state. |
| Cha | Constructs a new ChaCha random number generator with the specified seed and variant. |
| Cha | Initializes a new instance of the ChaCha class with the specified seed. |
| Cha | Initializes a new instance of the ChaCha class with the specified seed array. |
| Cha | Constructs a new ChaCha random number generator with the specified seed and variant. |
| Cha | Constructs a new ChaCha random number generator with the specified seed, stream ID, and variant. |
| Cha | Initializes a new instance of the ChaCha class with the specified seed. |
| Cha | Initializes a new instance of the ChaCha class with the specified seed array. |
Properties
| Bits |
Gets the number of bits per word in the generator's native output.
(Overrides RandomSource<TGenerator>.BitsPerWord) |
| Block |
Gets the number of output words per block produced by this generator.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Counter |
Gets the number of words in the engine counter.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Jump | Gets the size category of the primary jump operation. |
| Key |
Gets the number of words in the engine key.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Long | Gets the size category of the long jump operation. |
| Name |
Gets a human-readable name for this random number generator.
(Overrides RandomSource<TGenerator>.Name) |
| Rounds | Gets the number of rounds used in the ChaCha permutation. |
| State | Gets the size in bytes of the complete state of this random number generator. |
| Uncached |
Gets an uncached facade for this random number generator.
(Inherited from RandomSource<TGenerator>) |
Methods
| Advance | Advances the RNG state by the specified number of outputs. |
| Advance |
Advances the counter by the specified number of blocks.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Copy | Returns a deep copy of this RNG. |
| Create | Creates a new ChaCha instance from the specified options. |
| Create | Creates a jump-based stream partition for generating independent RNG streams. |
| Create | Creates a hierarchical stream tree for generating independent RNG streams. |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
| Fill( |
Fills a span with random 32-bit unsigned integer values.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 32-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 64-bit signed integers less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 32-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Fill( |
Fills a span with random 64-bit signed integers within a specified range.
(Inherited from RandomSource<TGenerator>) |
| From | Creates a new ChaCha instance with the specified key and nonce/counter initialized to zero. |
| From | Creates a new ChaCha instance with the specified key and nonce. |
| Generate |
Computes the output block for the current key and counter.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Get |
Copies the current counter into the provided destination span.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Get | Serves as the default hash function. (Inherited from Object) |
| Get |
Copies the current key into the provided destination span.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Get |
Returns the current state of the generator as a byte array.
(Inherited from RandomSource<TGenerator>) |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Jumped | Returns a new instance of the random number generator that is advanced by a specified number of jumps in the sequence. |
| Load | Loads the complete state of this random number generator from a byte span. |
| Long | Returns a new instance of the random number generator that is advanced by a specified number of long jumps in the sequence. |
| Next() |
Returns a non-negative random integer.
(Inherited from RandomSource<TGenerator>) |
| Next( |
Returns a non-negative random integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Next( |
Returns a random integer within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified array of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Fills the elements of a specified span of bytes with random numbers.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a non-negative random 64-bit integer.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a non-negative random 64-bit integer less than the specified maximum.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 64-bit integer within a specified range.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random single-precision floating-point number in the range [0.0, 1.0).
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 32-bit unsigned integer.
(Inherited from RandomSource<TGenerator>) |
| Next |
Returns a random 64-bit unsigned integer.
(Inherited from RandomSource<TGenerator>) |
| Reinitialize |
Reinitializes this random number generator using the specified initialization options.
(Inherited from RandomSource<TGenerator>) |
| Reset |
Resets the counter to all zeros.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Save | Saves the complete state of this random number generator to a byte span. |
| Set |
Sets the counter to the specified value.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| Set |
Sets the key to the specified value.
(Inherited from CounterBasedRandomSource<TGenerator, TWord>) |
| ToString | Returns a string that represents the current object. (Inherited from Object) |