SecureRandom.NetCore 2.0.0

dotnet add package SecureRandom.NetCore --version 2.0.0
                    
NuGet\Install-Package SecureRandom.NetCore -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SecureRandom.NetCore" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SecureRandom.NetCore" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SecureRandom.NetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SecureRandom.NetCore --version 2.0.0
                    
#r "nuget: SecureRandom.NetCore, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SecureRandom.NetCore@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SecureRandom.NetCore&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SecureRandom.NetCore&version=2.0.0
                    
Install as a Cake Tool

SecureRandom.NetCore

License: MIT NuGet

SecureRandom.NetCore is a cryptographic pseudo-random number generator (CPRNG) for .NET, based on Blake2b.NetCore and optimized to reduce memory exposure through PinnedMemory.

Table of contents

Runtime support

  • .NET 8 (net8.0)

Installation

.NET CLI

dotnet add package SecureRandom.NetCore

Package Manager Console

Install-Package SecureRandom.NetCore

NuGet

https://www.nuget.org/packages/SecureRandom.NetCore/

Quick start

using SecureRandom.NetCore;

using var cprng = new SecureRandom();

// Generate a new byte array
byte[] token = cprng.NextBytes(32);

// Fill an existing buffer
byte[] nonce = new byte[12];
cprng.NextBytes(nonce);

// Random primitives
int count = cprng.Next(1, 100);
long id = cprng.NextLong();
double value = cprng.NextDouble();

Security notes

  • By default, the constructor auto-seeds from the OS entropy provider (RandomNumberGenerator.Fill).
  • Avoid seed: false unless you explicitly provide secure seed material via SetSeed(...) before calling generation APIs.
  • SecureRandom implements IDisposable; always dispose instances to clear and release internal state.

API reference

Constructor

SecureRandom(int rounds = 10, bool seed = true)
  • rounds: Number of state generations before cycling seed material.
  • seed: When true (default), instance self-seeds with OS entropy.

⚠️ seed: false creates an unseeded generator. Calling random generation methods before SetSeed(...) will throw.

Methods

Method Description
int GetSeedSize() Gets digest output length used for internal state and seed buffers.
void SetSeed(byte[] seed) Mixes user-provided byte seed material into internal seed state.
void SetSeed(long seed) Mixes user-provided integral seed material into internal seed state.
int Next() Returns a non-negative random int.
int Next(int maxValue) Returns random int in [0, maxValue).
int Next(int minValue, int maxValue) Returns random int in [minValue, maxValue).
byte[] NextBytes(int length) Creates and returns a random byte array of length.
void NextBytes(byte[] bytes) Fills provided byte array with random data.
void NextBytes(byte[] bytes, int offset, int length) Fills a segment of a byte array with random data.
double NextDouble() Returns random double in range [0, 1].
int NextInt() Returns random int over full signed 32-bit range.
long NextLong() Returns random long over full signed 64-bit range.
void Dispose() Disposes digest/state resources and suppresses finalization.

Examples project

Sample usage is available in:

  • SecureRandom.NetCore.Examples/Program.cs

Run it with:

dotnet run --project SecureRandom.NetCore.Examples

Testing

This repository includes unit tests in SecureRandom.NetCore.Tests.

Run all tests:

dotnet test SecureRandom.NetCore.sln

Contributing

Issues and pull requests are welcome.

  1. Fork the repository.
  2. Create a feature branch.
  3. Add or update tests.
  4. Run dotnet test.
  5. Open a pull request with a clear summary.

License

MIT. See LICENSE.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on SecureRandom.NetCore:

Package Downloads
Curve25519.NetCore

An elliptic curve offering 128 bits of security and designed for use with the elliptic curve Diffie–Hellman (ECDH) key agreement scheme. It is one of the fastest ECC curves and is not covered by any known patents.

BigInteger.NetCore

Implementation of a arbitrary-precision arithmetic method using SecureRandom.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 85 2/24/2026
1.0.0 15,254 7/27/2020