Skip to content

OnixLabs.Security.Cryptography.Salt

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

Salt Struct

Represents a cryptographically secure random number, otherwise known as a salt value.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → ValueType → Salt
Implements ICryptoPrimitive<Salt>, IValueEquatable<Salt>, IEquatable<Salt>, IBinaryConvertible

Definition

public readonly partial struct Salt : ICryptoPrimitive<Salt>

Remarks

Salt wraps an immutable byte array representing a cryptographically secure random value. Instances are constructed from existing byte data via the public constructors, or generated from a cryptographically secure random number generator via the Create and CreateNonZero factory methods.

All constructors accept their input as ReadOnlySpan<byte>, ReadOnlyMemory<byte>, or ReadOnlySequence<byte> and immediately copy the provided bytes into a private array, ensuring that the Salt cannot be mutated after construction through an external reference.

Equality is implemented via ICryptoPrimitive<Salt> → IValueEquatable<Salt> → IEquatable<Salt>, and uses constant-time byte comparison (CryptographicOperations.FixedTimeEquals) to prevent timing-based side-channel attacks. Two Salt values are equal if and only if their underlying byte arrays have identical length and content.

ToString() and ToString(IFormatProvider) encode the underlying bytes using a base-encoded format. The default (no-argument) overload uses lowercase hexadecimal (Base16FormatProvider.Invariant). Passing a different IFormatProvider — such as Base58FormatProvider.Bitcoin or Base64FormatProvider.Invariant — selects the corresponding encoding. The AsReadOnlySpan() and AsReadOnlyMemory() members satisfy IBinaryConvertible and provide zero-allocation access to the underlying data.

Note A default-initialized Salt (default(Salt)) has a null internal array and a Length of 0. Comparisons involving a default instance are safe; two default instances are considered equal.

Examples

Generating a random salt

using OnixLabs.Security.Cryptography;

// Create a 32-byte cryptographically random salt (may contain zero bytes).
Salt salt = Salt.Create(32);
Console.WriteLine(salt.Length);   // 32
Console.WriteLine(salt);          // e.g. 3a7f... (64 hex characters)

// Create a 16-byte salt that contains no zero bytes.
Salt nonZero = Salt.CreateNonZero(16);

Constructing from existing bytes

using OnixLabs.Security.Cryptography;

byte[] existing = { 0x01, 0x02, 0x03, 0x04 };
Salt salt = new Salt(existing.AsSpan());

Console.WriteLine(salt);          // 01020304

Encoding with a different format provider

using OnixLabs.Core.Text;
using OnixLabs.Security.Cryptography;

Salt salt = Salt.Create(16);

string hex    = salt.ToString(Base16FormatProvider.Invariant);
string base64 = salt.ToString(Base64FormatProvider.Invariant);

Console.WriteLine(hex);    // lowercase hex
Console.WriteLine(base64); // base-64 encoded

Constant-time equality

using OnixLabs.Security.Cryptography;

Salt a = Salt.Create(32);
Salt b = new Salt(a.AsReadOnlySpan()); // copy of the same bytes

Console.WriteLine(a == b); // True  (constant-time comparison)
Console.WriteLine(a != b); // False

Static Methods

Method Returns Description
Create(int) Salt Creates a new Salt of the specified length filled with cryptographically random bytes.
CreateNonZero(int) Salt Creates a new Salt of the specified length filled with cryptographically random non-zero bytes.

Create(int)

public static Salt Create(int length)

Creates a new Salt instance of the specified length, filled with cryptographically random bytes sourced from RandomNumberGenerator. The resulting bytes may include zero bytes.

Parameter Type Description
length int The number of random bytes to generate.

Returns — A new Salt containing length cryptographically random bytes.

Exceptions

Exception Condition
ArgumentOutOfRangeException length is negative.

CreateNonZero(int)

public static Salt CreateNonZero(int length)

Creates a new Salt instance of the specified length, filled with cryptographically random bytes that are guaranteed to be non-zero. Internally uses RandomNumberGenerator.GetNonZeroBytes.

Parameter Type Description
length int The number of random non-zero bytes to generate.

Returns — A new Salt containing length cryptographically random non-zero bytes.

Exceptions

Exception Condition
ArgumentOutOfRangeException length is negative.

Constructors

Constructor Description
Salt(ReadOnlySpan<byte>) Initializes a new instance from a ReadOnlySpan<byte>.
Salt(ReadOnlyMemory<byte>) Initializes a new instance from a ReadOnlyMemory<byte>.
Salt(ReadOnlySequence<byte>) Initializes a new instance from a ReadOnlySequence<byte>.

Salt(ReadOnlySpan<byte>)

public Salt(ReadOnlySpan<byte> value)

Initializes a new instance of the Salt struct from the specified ReadOnlySpan<byte>. The bytes are copied into a private array; subsequent mutation of the source does not affect the Salt.

Parameter Type Description
value ReadOnlySpan<byte> The byte span from which to initialize the salt.

Salt(ReadOnlyMemory<byte>)

public Salt(ReadOnlyMemory<byte> value)

Initializes a new instance of the Salt struct from the specified ReadOnlyMemory<byte>. The bytes are copied into a private array.

Parameter Type Description
value ReadOnlyMemory<byte> The byte memory from which to initialize the salt.

Salt(ReadOnlySequence<byte>)

public Salt(ReadOnlySequence<byte> value)

Initializes a new instance of the Salt struct from the specified ReadOnlySequence<byte>. The bytes are copied into a private array.

Parameter Type Description
value ReadOnlySequence<byte> The byte sequence from which to initialize the salt.

Properties

Property Type Description
Length int Gets the length of the current Salt in bytes.

Length

public int Length { get; }

Gets the number of bytes in the underlying salt value. Returns 0 for a default-initialized Salt.

Methods

Method Returns Description
AsReadOnlyMemory() ReadOnlyMemory<byte> Returns the underlying bytes as a ReadOnlyMemory<byte>.
AsReadOnlySpan() ReadOnlySpan<byte> Returns the underlying bytes as a ReadOnlySpan<byte>.
Equals(Salt) bool Determines whether this instance equals another Salt using constant-time comparison.
Equals(object?) bool Determines whether this instance equals another object.
GetHashCode() int Returns a hash code derived from the content of the underlying byte array.
ToString() string Returns the underlying bytes encoded as lowercase hexadecimal.
ToString(IFormatProvider) string Returns the underlying bytes encoded using the specified format provider.

AsReadOnlyMemory()

public ReadOnlyMemory<byte> AsReadOnlyMemory()

Returns the underlying salt bytes as a ReadOnlyMemory<byte>. Satisfies IBinaryConvertible.

Returns — A ReadOnlyMemory<byte> over the underlying byte array.


AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

Returns the underlying salt bytes as a ReadOnlySpan<byte>. Satisfies IBinaryConvertible.

Returns — A ReadOnlySpan<byte> over the underlying byte array.


Equals(Salt)

public bool Equals(Salt other)

Determines whether this instance is equal to other using constant-time byte comparison (CryptographicOperations.FixedTimeEquals). Two Salt values are equal when their underlying byte arrays have identical length and content. Two default-initialized values are considered equal.

Parameter Type Description
other Salt The value to compare with this instance.

Returns — true if the two values are equal; otherwise, false.


Equals(object?)

public override bool Equals(object? obj)

Determines whether this instance is equal to another object.

Parameter Type Description
obj object? The object to compare with this instance.

Returns — true if obj is a Salt equal to this instance; otherwise, false.


GetHashCode()

public override int GetHashCode()

Returns a hash code derived from the content of the underlying byte array. 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 bytes encoded as lowercase hexadecimal using Base16FormatProvider.Invariant.

Returns — A lowercase hexadecimal string representation of the salt bytes.


ToString(IFormatProvider)

public string ToString(IFormatProvider provider)

Returns the underlying bytes encoded using the specified format provider. Satisfies ICryptoPrimitive<Salt>.

Parameter Type Description
provider IFormatProvider The format provider that selects the base encoding, such as Base16FormatProvider.Invariant, Base58FormatProvider.Bitcoin, or Base64FormatProvider.Invariant.

Returns — A string representation of the salt bytes in the encoding selected by provider.

Operators

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

operator ==

public static bool operator ==(Salt left, Salt right)

Determines whether two Salt values are equal. Delegates to Equals(Salt), which uses constant-time comparison.

operator !=

public static bool operator !=(Salt left, Salt right)

Determines whether two Salt values are not equal. Returns true if left is not equal to right; otherwise, false.

See Also

ONIXLabs .NET Library


Clone this wiki locally