Skip to content

OnixLabs.Security.SecurityToken

Matthew Layton edited this page Nov 15, 2026 · 4 revisions

SecurityToken Struct

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>

Definition

public readonly partial struct SecurityToken(ReadOnlySpan<char> value) : IValueEquatable<SecurityToken>

Remarks

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 SecurityToken is not a constant-time operation. Do not rely on it for timing-attack-resistant comparison of secrets.

Examples

Creating and comparing tokens

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-T0k3n

Building a token

using OnixLabs.Security;

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

Constructors

Constructor Description
SecurityToken(ReadOnlySpan<char>) Initializes a new instance of the SecurityToken struct from the specified characters.

SecurityToken(ReadOnlySpan<char>)

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.

Properties

Property Type Description
Length int Gets the number of characters in the underlying security token value.

Length

public int Length { get; }

Gets the length of the underlying security token value. Returns 0 for a default instance.

Methods

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.

Equals(SecurityToken)

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.

Equals(object?)

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.

GetHashCode()

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.

ToString()

public override string ToString()

Returns the underlying token value as a string, or string.Empty for a default instance.

Returns — The underlying token value.

Operators

Operator Description
operator == Determines whether two tokens are equal.
operator != Determines whether two tokens are not equal.

operator ==

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.

operator !=

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.

See Also

ONIXLabs .NET Library


Clone this wiki locally