Repository navigation
OnixLabs.Security.SecurityTokenBuilder
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
|
public sealed class SecurityTokenBuilderSecurityTokenBuilder 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:
-
CreateSecureRandom— backed byRandomNumberGenerator. Use this for any token that protects a real asset. -
CreatePseudoRandom— backed byRandom. Convenient and seedable for tests and reproducible output, but not cryptographically secure.
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
CreatePseudoRandomusesRandom, which is not cryptographically secure and produces predictable output. Do not use it to generate tokens in production — preferCreateSecureRandom.
Behavioural notes:
- The factory methods throw
ArgumentOutOfRangeExceptionwhenlengthis negative. -
ToSecurityTokenthrowsInvalidOperationExceptionwhen no character set has been specified. - A
lengthof0is valid and produces an empty token (provided a character set has been specified).
using OnixLabs.Security;
SecurityToken token = SecurityTokenBuilder
.CreateSecureRandom(32)
.UseAlphaNumericCharacters()
.ToSecurityToken();using OnixLabs.Security;
SecurityToken token = SecurityTokenBuilder
.CreateSecureRandom(16)
.UseUpperCase()
.UseDigits()
.UseBasicSpecialCharacters()
.ToSecurityToken();using OnixLabs.Security;
// Seeded pseudo-random source — deterministic, NOT for production use.
SecurityToken token = SecurityTokenBuilder
.CreatePseudoRandom(8, seed: 12345)
.UseLowerCase()
.ToSecurityToken();| 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. |
| 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. |
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. |
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. |
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. |
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.
public SecurityTokenBuilder UseLowerCase()Allows the lowercase characters abcdefghijklmnopqrstuvwxyz in generated tokens.
Returns — The current SecurityTokenBuilder instance.
public SecurityTokenBuilder UseUpperCase()Allows the uppercase characters ABCDEFGHIJKLMNOPQRSTUVWXYZ in generated tokens.
Returns — The current SecurityTokenBuilder instance.
public SecurityTokenBuilder UseDigits()Allows the digit characters 0123456789 in generated tokens.
Returns — The current SecurityTokenBuilder instance.
public SecurityTokenBuilder UseBasicSpecialCharacters()Allows the basic special characters !@#$%^&* in generated tokens.
Returns — The current SecurityTokenBuilder instance.
public SecurityTokenBuilder UseExtendedSpecialCharacters()Allows the extended special characters !@#$%^&*()_+-={}[]:;"'<>,.?/~`|\ in generated tokens.
Returns — The current SecurityTokenBuilder instance.
public SecurityTokenBuilder UseAlphaNumericCharacters()Allows alphanumeric characters in generated tokens — equivalent to calling UseLowerCase, UseUpperCase, and UseDigits.
Returns — The current SecurityTokenBuilder instance.
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). |
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions