Skip to content

OnixLabs.Security.SecurityTokenBuilder

matthew edited this page May 27, 2026 · 2 revisions

SecurityTokenBuilder Class

A fluent builder that assembles a SecurityToken of a fixed length from a configurable character set, drawing characters from a pseudo-random or cryptographically secure random number source.

Namespace OnixLabs.Security
Assembly OnixLabs.Security.dll (NuGet)
Inheritance object → SecurityTokenBuilder

Definition

public sealed class SecurityTokenBuilder

Remarks

SecurityTokenBuilder has no public constructor. Obtain an instance from one of the static factory methods, each of which fixes the token length and selects the random number source:

After construction, call the fluent Use… methods to accumulate the set of characters a generated token may contain; each returns the same builder so calls can be chained. The character set is a set — adding the same character more than once (for example via overlapping Use… calls) has no additional effect. Finally, ToSecurityToken draws length characters uniformly at random from the configured set and returns the resulting SecurityToken.

Warning CreatePseudoRandom uses Random, which is not cryptographically secure and produces predictable output. Do not use it to generate tokens in production — prefer CreateSecureRandom.

Behavioural notes:

Examples

Generating a secure alphanumeric token

using OnixLabs.Security;

SecurityToken token = SecurityTokenBuilder
    .CreateSecureRandom(32)
    .UseAlphaNumericCharacters()
    .ToSecurityToken();

Composing a custom character set

using OnixLabs.Security;

SecurityToken token = SecurityTokenBuilder
    .CreateSecureRandom(16)
    .UseUpperCase()
    .UseDigits()
    .UseBasicSpecialCharacters()
    .ToSecurityToken();

Reproducible tokens for tests

using OnixLabs.Security;

// Seeded pseudo-random source — deterministic, NOT for production use.
SecurityToken token = SecurityTokenBuilder
    .CreatePseudoRandom(8, seed: 12345)
    .UseLowerCase()
    .ToSecurityToken();

Methods

Static Methods

Method Returns Description
CreatePseudoRandom(int, Random?) SecurityTokenBuilder Creates a builder using a pseudo-random number provider.
CreatePseudoRandom(int, int) SecurityTokenBuilder Creates a builder using a pseudo-random number provider seeded with the specified value.
CreateSecureRandom(int) SecurityTokenBuilder Creates a builder using a cryptographically secure random number provider.

Instance Methods

Method Returns Description
UseCharacters(ReadOnlySpan<char>) SecurityTokenBuilder Allows the specified arbitrary characters.
UseLowerCase() SecurityTokenBuilder Allows lowercase characters.
UseUpperCase() SecurityTokenBuilder Allows uppercase characters.
UseDigits() SecurityTokenBuilder Allows digit characters.
UseBasicSpecialCharacters() SecurityTokenBuilder Allows basic special characters.
UseExtendedSpecialCharacters() SecurityTokenBuilder Allows extended special characters.
UseAlphaNumericCharacters() SecurityTokenBuilder Allows lowercase, uppercase, and digit characters.
ToSecurityToken() SecurityToken Builds a new token from the configured character set.

CreatePseudoRandom(int, Random?)

public static SecurityTokenBuilder CreatePseudoRandom(int length, Random? random = null)

Creates a new SecurityTokenBuilder that draws characters from a pseudo-random number provider. When random is null, a new Random instance is used. Not cryptographically secure — see the Remarks.

Parameter Type Description
length int The desired length of the generated security token.
random Random? The underlying pseudo-random instance. Defaults to null, in which case a new Random is created.

Returns — A new SecurityTokenBuilder instance using a pseudo-random number provider.

Exceptions

Exception Condition
ArgumentOutOfRangeException length is negative.

CreatePseudoRandom(int, int)

public static SecurityTokenBuilder CreatePseudoRandom(int length, int seed)

Creates a new SecurityTokenBuilder that draws characters from a pseudo-random number provider seeded with seed, producing deterministic output for a given seed. Not cryptographically secure — see the Remarks.

Parameter Type Description
length int The desired length of the generated security token.
seed int The seed for the pseudo-random number provider.

Returns — A new SecurityTokenBuilder instance using a seeded pseudo-random number provider.

Exceptions

Exception Condition
ArgumentOutOfRangeException length is negative.

CreateSecureRandom(int)

public static SecurityTokenBuilder CreateSecureRandom(int length)

Creates a new SecurityTokenBuilder that draws characters from a cryptographically secure random number provider (RandomNumberGenerator). This is the recommended factory for production use.

Parameter Type Description
length int The desired length of the generated security token.

Returns — A new SecurityTokenBuilder instance using a secure random number provider.

Exceptions

Exception Condition
ArgumentOutOfRangeException length is negative.

UseCharacters(ReadOnlySpan<char>)

public SecurityTokenBuilder UseCharacters(ReadOnlySpan<char> allowedCharacters)

Adds the specified arbitrary characters to the set of characters a generated token may contain. Duplicate characters are ignored.

Parameter Type Description
allowedCharacters ReadOnlySpan<char> The characters that are allowed as part of a generated security token.

Returns — The current SecurityTokenBuilder instance.

UseLowerCase()

public SecurityTokenBuilder UseLowerCase()

Allows the lowercase characters abcdefghijklmnopqrstuvwxyz in generated tokens.

Returns — The current SecurityTokenBuilder instance.

UseUpperCase()

public SecurityTokenBuilder UseUpperCase()

Allows the uppercase characters ABCDEFGHIJKLMNOPQRSTUVWXYZ in generated tokens.

Returns — The current SecurityTokenBuilder instance.

UseDigits()

public SecurityTokenBuilder UseDigits()

Allows the digit characters 0123456789 in generated tokens.

Returns — The current SecurityTokenBuilder instance.

UseBasicSpecialCharacters()

public SecurityTokenBuilder UseBasicSpecialCharacters()

Allows the basic special characters !@#$%^&* in generated tokens.

Returns — The current SecurityTokenBuilder instance.

UseExtendedSpecialCharacters()

public SecurityTokenBuilder UseExtendedSpecialCharacters()

Allows the extended special characters !@#$%^&*()_+-={}[]:;"'<>,.?/~`|\ in generated tokens.

Returns — The current SecurityTokenBuilder instance.

UseAlphaNumericCharacters()

public SecurityTokenBuilder UseAlphaNumericCharacters()

Allows alphanumeric characters in generated tokens — equivalent to calling UseLowerCase, UseUpperCase, and UseDigits.

Returns — The current SecurityTokenBuilder instance.

ToSecurityToken()

public SecurityToken ToSecurityToken()

Builds a new SecurityToken of the configured length, selecting each character uniformly at random from the configured character set using the builder's random number source.

Returns — A new SecurityToken instance.

Exceptions

Exception Condition
InvalidOperationException No character set has been specified (no Use… method was called).

See Also

ONIXLabs .NET Library


Clone this wiki locally