RNGCryptoServiceProvider.GetNonZeroBytes Method

Definition

Overloads

GetNonZeroBytes(Byte[])

Fills an array of bytes with a cryptographically strong sequence of random nonzero values.

GetNonZeroBytes(Span<Byte>)

Fills a byte span with a cryptographically strong random sequence of nonzero values.

GetNonZeroBytes(Byte[])

Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs

Fills an array of bytes with a cryptographically strong sequence of random nonzero values.

C#
public override void GetNonZeroBytes(byte[] data);

Parameters

data
Byte[]

The array to fill with a cryptographically strong sequence of random nonzero values.

Exceptions

The cryptographic service provider (CSP) cannot be acquired.

data is null.

Examples

The following code example creates a random sequence of 100 nonzero bytes and stores it in random.

C#
byte[] random = new byte[100];

using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
    rng.GetNonZeroBytes(random); // The array is now filled with cryptographically strong random bytes, and none are zero.
}

Remarks

The length of the byte array determines how many cryptographically strong random nonzero bytes are produced.

See also

Applies to

.NET 10 i druge verzije
Proizvod Verzije
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetNonZeroBytes(Span<Byte>)

Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs

Fills a byte span with a cryptographically strong random sequence of nonzero values.

C#
public override void GetNonZeroBytes(Span<byte> data);

Parameters

data
Span<Byte>

The span to fill with cryptographically strong random nonzero bytes.

Applies to

.NET 10 i druge verzije
Proizvod Verzije
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1