SecureRandom.NetCore
2.0.0
dotnet add package SecureRandom.NetCore --version 2.0.0
NuGet\Install-Package SecureRandom.NetCore -Version 2.0.0
<PackageReference Include="SecureRandom.NetCore" Version="2.0.0" />
<PackageVersion Include="SecureRandom.NetCore" Version="2.0.0" />
<PackageReference Include="SecureRandom.NetCore" />
paket add SecureRandom.NetCore --version 2.0.0
#r "nuget: SecureRandom.NetCore, 2.0.0"
#:package SecureRandom.NetCore@2.0.0
#addin nuget:?package=SecureRandom.NetCore&version=2.0.0
#tool nuget:?package=SecureRandom.NetCore&version=2.0.0
SecureRandom.NetCore
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
- Installation
- Quick start
- Security notes
- API reference
- Examples project
- Testing
- Contributing
- License
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: falseunless you explicitly provide secure seed material viaSetSeed(...)before calling generation APIs. SecureRandomimplementsIDisposable; 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: Whentrue(default), instance self-seeds with OS entropy.
⚠️
seed: falsecreates an unseeded generator. Calling random generation methods beforeSetSeed(...)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.
- Fork the repository.
- Create a feature branch.
- Add or update tests.
- Run
dotnet test. - Open a pull request with a clear summary.
License
MIT. See LICENSE.
| Product | Versions 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. |
-
net8.0
- Blake2b.NetCore (>= 1.0.3)
- PinnedMemory (>= 2.0.1)
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.