Repository navigation
OnixLabs.Security.SecurityToken
Represents a security token — an immutable, opaque sequence of characters (such as an API key, nonce, or one-time code) with content-based value equality.
| Namespace | OnixLabs.Security |
| Assembly | OnixLabs.Security.dll (NuGet) |
| Inheritance |
object → ValueType → SecurityToken
|
| Implements | IValueEquatable<SecurityToken> |
public readonly partial struct SecurityToken(ReadOnlySpan<char> value) : IValueEquatable<SecurityToken>SecurityToken is a lightweight wrapper around a sequence of characters, intended to model a security-sensitive value as a distinct type rather than a bare string. The value is captured from the constructor's ReadOnlySpan<char> and held internally as a string.
Equality is value-based, in line with IValueEquatable<T>: two tokens are equal when their character sequences are equal, and GetHashCode is derived from the content. This means equal tokens produce equal hash codes and can be used safely as dictionary keys or set elements.
The default(SecurityToken) value has no underlying string; it behaves as an empty token — Length returns 0 and ToString returns string.Empty.
Tokens are usually produced by SecurityTokenBuilder, which assembles a token of a given length from a configurable character set using a pseudo-random or cryptographically secure random number source.
Note Because equality and hashing read the full token content, comparing or hashing a
SecurityTokenis not a constant-time operation. Do not rely on it for timing-attack-resistant comparison of secrets.
using OnixLabs.Security;
SecurityToken a = new("S3cur3-T0k3n".AsSpan());
SecurityToken b = new("S3cur3-T0k3n".AsSpan());
Console.WriteLine(a == b); // True (value equality)
Console.WriteLine(a.Length); // 12
Console.WriteLine(a.ToString()); // S3cur3-T0k3nusing OnixLabs.Security;
SecurityToken token = SecurityTokenBuilder
.CreateSecureRandom(32)
.UseAlphaNumericCharacters()
.ToSecurityToken();| Constructor | Description |
|---|---|
SecurityToken(ReadOnlySpan<char>) |
Initializes a new instance of the SecurityToken struct from the specified characters. |
public SecurityToken(ReadOnlySpan<char> value)Initializes a new instance of the SecurityToken struct, copying the specified characters into the token.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The underlying value of the security token. |
| Property | Type | Description |
|---|---|---|
Length |
int |
Gets the number of characters in the underlying security token value. |
public int Length { get; }Gets the length of the underlying security token value. Returns 0 for a default instance.
| Method | Returns | Description |
|---|---|---|
Equals(SecurityToken) |
bool |
Determines whether this token equals another token. |
Equals(object?) |
bool |
Determines whether this token equals another object. |
GetHashCode() |
int |
Returns a content-derived hash code. |
ToString() |
string |
Returns the underlying token value. |
public bool Equals(SecurityToken other)Determines whether the current token is equal to another token of the same type by comparing the underlying character sequences. Two default (empty) tokens are considered equal.
| Parameter | Type | Description |
|---|---|---|
other |
SecurityToken |
The token to compare with the current token. |
Returns — true if the tokens have equal content; otherwise, false.
public override bool Equals(object? obj)Determines whether the current token is equal to another object.
| Parameter | Type | Description |
|---|---|---|
obj |
object? |
The object to compare with the current token. |
Returns — true if obj is a SecurityToken with equal content; otherwise, false.
public override int GetHashCode()Returns a hash code derived from the token's character content, consistent with Equals(SecurityToken). Equal values produce equal hash codes; like HashCode, the result is randomised per process and must not be persisted.
Returns — A 32-bit signed integer hash code.
public override string ToString()Returns the underlying token value as a string, or string.Empty for a default instance.
Returns — The underlying token value.
| Operator | Description |
|---|---|
operator == |
Determines whether two tokens are equal. |
operator != |
Determines whether two tokens are not equal. |
public static bool operator ==(SecurityToken left, SecurityToken right)Performs an equality comparison between two tokens. Returns true if left has the same content as right; otherwise, false.
public static bool operator !=(SecurityToken left, SecurityToken right)Performs an inequality comparison between two tokens. Returns true if left does not have the same content as right; otherwise, false.
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