SecureTokenGeneratR 2.0.0-alpha.1

This is a prerelease version of SecureTokenGeneratR.
dotnet add package SecureTokenGeneratR --version 2.0.0-alpha.1
                    
NuGet\Install-Package SecureTokenGeneratR -Version 2.0.0-alpha.1
                    
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="SecureTokenGeneratR" Version="2.0.0-alpha.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SecureTokenGeneratR" Version="2.0.0-alpha.1" />
                    
Directory.Packages.props
<PackageReference Include="SecureTokenGeneratR" />
                    
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 SecureTokenGeneratR --version 2.0.0-alpha.1
                    
#r "nuget: SecureTokenGeneratR, 2.0.0-alpha.1"
                    
#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 SecureTokenGeneratR@2.0.0-alpha.1
                    
#: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=SecureTokenGeneratR&version=2.0.0-alpha.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SecureTokenGeneratR&version=2.0.0-alpha.1&prerelease
                    
Install as a Cake Tool

SecureTokenGeneratR

Build status

SecureTokenGeneratR generates random tokens using .NET's cryptographically secure random number generator. It has no external dependencies.

Install

dotnet add package SecureTokenGeneratR

Defaults

By default, generated tokens:

  • Are 16 characters long.
  • Include at least one character from each set: lowercase letters, uppercase letters, digits, and ASCII punctuation.
  • Have no limit on repeated characters.

Usage

Default generator

Use SecureToken.Default to generate tokens with the default settings:

using SecureTokenGeneratR;

string token = SecureToken.Default.Generate();

Fluent configuration

Each call returns a new generator, so options can be chained:

Call Effect
SecureToken.Default Starts with the default length and character sets.
WithLength(length) Sets a fixed token length.
WithLength(minimum, maximum) Randomly chooses an inclusive length between minimum and maximum for each generated token.
WithMaxRepeatingCharCount(count) Limits how many times any one character may appear; 0 means no limit.
AddCharset(sequence) Adds a character sequence to the current allowed sets. When called on SecureToken, starts with the default sets.
AddCharset(start, end) Adds an inclusive Unicode range to the current allowed sets. When called on SecureToken, starts with the default sets.
ReplaceCharsets(sequence) Replaces all current allowed sets with one character sequence.
ReplaceCharsets(start, end) Replaces all current allowed sets with one inclusive Unicode range.
ReplaceCharsets(charsets) Replaces all current allowed sets with the supplied ICharacterSet collection.
Generate() Generates a token using the configured options.

AddCharset adds; ReplaceCharsets replaces. Generated tokens include at least one character from each configured set.

For example, this generator adds two sets and picks a random token length from 8 to 24. The second generator replaces those sets with digits:

using SecureTokenGeneratR;

var generator = SecureToken.AddCharset("%XW-[]{}") // character sequence
    .AddCharset('A', 'F') // inclusive Unicode range: U+0041 to U+0046
    .WithLength(8, 24)
    .WithMaxRepeatingCharCount(2);

string token = generator.Generate();
var digitsOnlyGenerator = generator.ReplaceCharsets('0', '9');

For multiple custom sets, construct each with CharacterSet and pass them to ReplaceCharsets:

using SecureTokenGeneratR.CharacterSets;

ICharacterSet[] charsets =
[
    new CharacterSet("abc"), // character sequence
    new CharacterSet('0', '9') // inclusive Unicode range: U+0030 to U+0039
];
var customGenerator = SecureToken.Default.ReplaceCharsets(charsets);

Entropy

The generator exposes estimates for token entropy and per-character entropy:

var generator = SecureToken.Default;
string token = generator.Generate();

double tokenEntropy = generator.GetTokenEntropy;
double characterEntropy = generator.GetCharacterEntropy(token);

Entropy is one measure of strength and should not be used as the only security assessment.

Compatibility

Targets .NET Framework 4.6.1, .NET Standard 2.0 and 2.1, and .NET 6, 8, and 10.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-alpha.1 0 10/24/2026
1.0.1 37,220 10/17/2021
1.0.0 756 10/10/2021

* First alpha release of 2.0.0