Repository navigation
OnixLabs.Security.Cryptography.Salt
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
|
public readonly partial struct Salt : ICryptoPrimitive<Salt>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 anullinternal array and aLengthof0. Comparisons involving a default instance are safe; two default instances are considered equal.
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);using OnixLabs.Security.Cryptography;
byte[] existing = { 0x01, 0x02, 0x03, 0x04 };
Salt salt = new Salt(existing.AsSpan());
Console.WriteLine(salt); // 01020304using 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 encodedusing 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| 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. |
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. |
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. |
| 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>. |
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. |
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. |
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. |
| Property | Type | Description |
|---|---|---|
Length |
int |
Gets the length of the current Salt in bytes. |
public int Length { get; }Gets the number of bytes in the underlying salt value. Returns 0 for a default-initialized Salt.
| 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. |
public ReadOnlyMemory<byte> AsReadOnlyMemory()Returns the underlying salt bytes as a ReadOnlyMemory<byte>. Satisfies IBinaryConvertible.
Returns — A ReadOnlyMemory<byte> over the underlying byte array.
public ReadOnlySpan<byte> AsReadOnlySpan()Returns the underlying salt bytes as a ReadOnlySpan<byte>. Satisfies IBinaryConvertible.
Returns — A ReadOnlySpan<byte> over the underlying byte array.
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.
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.
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.
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.
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.
| Operator | Description |
|---|---|
operator == |
Determines whether two Salt values are equal. |
operator != |
Determines whether two Salt values are not equal. |
public static bool operator ==(Salt left, Salt right)Determines whether two Salt values are equal. Delegates to Equals(Salt), which uses constant-time comparison.
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.
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