Skip to content

OnixLabs.Security.Cryptography.DigitalSignature

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

DigitalSignature Struct

Represents a cryptographic digital signature.

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

Definition

public readonly partial struct DigitalSignature :
    ICryptoPrimitive<DigitalSignature>,
    ISpanParsable<DigitalSignature>

Remarks

DigitalSignature wraps an immutable byte array that holds the raw output bytes of a cryptographic signing operation. Instances are typically produced by a key type such as EcdsaPrivateKey or RsaPrivateKey and subsequently verified using the corresponding public key.

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 DigitalSignature value is immutable after construction. Three implicit conversion operators (one per source type) allow assignment directly from the appropriate BCL span/memory/sequence types.

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

ISpanParsable<DigitalSignature> allows a base-encoded string (for example, a hex-encoded signature stored in a database) to be round-tripped back into a DigitalSignature. When no IFormatProvider is supplied, Base16FormatProvider.Invariant (lowercase hexadecimal) is used for both serialisation and deserialisation.

ToString() and ToString(IFormatProvider) encode the underlying bytes in the selected base format. The AsReadOnlySpan() and AsReadOnlyMemory() members satisfy IBinaryConvertible and provide direct, zero-allocation access to the underlying data.

Note A default-initialized DigitalSignature (default(DigitalSignature)) has a null internal array. Comparisons involving a default instance are safe; two default instances are considered equal.

Examples

Signing and verifying with ECDSA

using OnixLabs.Security.Cryptography;

// Obtain a key pair (key generation shown as illustrative; use the real API for your key type).
EcdsaPrivateKey privateKey = EcdsaPrivateKey.Create();
NamedPublicKey publicKey = privateKey.GetPublicKey();

byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello, World!");

// Sign the data — the private key returns a DigitalSignature.
DigitalSignature signature = privateKey.SignData(data);

// Persist the signature as hex.
string hex = signature.ToString();
Console.WriteLine(hex);

// Restore from hex and verify.
DigitalSignature restored = DigitalSignature.Parse(hex);
bool valid = publicKey.IsValidSignature(restored, data);
Console.WriteLine(valid); // True

Constructing from raw bytes

using OnixLabs.Security.Cryptography;

byte[] raw = GetSignatureBytesFromStorage();
DigitalSignature signature = new DigitalSignature(raw.AsSpan());

Implicit conversion

using OnixLabs.Security.Cryptography;

ReadOnlyMemory<byte> memory = GetSignatureMemory();
DigitalSignature signature = memory; // implicit operator

Encoding with a different format provider

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

DigitalSignature signature = GetSignature();
string base64 = signature.ToString(Base64FormatProvider.Invariant);

Constructors

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

DigitalSignature(ReadOnlySpan<byte>)

public DigitalSignature(ReadOnlySpan<byte> value)

Initializes a new instance of the DigitalSignature struct. The bytes are copied into a private array; subsequent mutation of the source does not affect the DigitalSignature.

Parameter Type Description
value ReadOnlySpan<byte> The byte span containing the raw signature bytes.

DigitalSignature(ReadOnlyMemory<byte>)

public DigitalSignature(ReadOnlyMemory<byte> value)

Initializes a new instance of the DigitalSignature struct from a ReadOnlyMemory<byte>.

Parameter Type Description
value ReadOnlyMemory<byte> The byte memory containing the raw signature bytes.

DigitalSignature(ReadOnlySequence<byte>)

public DigitalSignature(ReadOnlySequence<byte> value)

Initializes a new instance of the DigitalSignature struct from a ReadOnlySequence<byte>.

Parameter Type Description
value ReadOnlySequence<byte> The byte sequence containing the raw signature bytes.

Static Methods

Method Returns Description
Parse(string, IFormatProvider?) DigitalSignature Parses a base-encoded string into a DigitalSignature.
Parse(ReadOnlySpan<char>, IFormatProvider?) DigitalSignature Parses a base-encoded character span into a DigitalSignature.
TryParse(string?, IFormatProvider?, out DigitalSignature) bool Tries to parse a base-encoded string into a DigitalSignature.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out DigitalSignature) bool Tries to parse a base-encoded character span into a DigitalSignature.

Parse(string, IFormatProvider?)

public static DigitalSignature Parse(string value, IFormatProvider? provider = null)

Parses a base-encoded string into a DigitalSignature. When provider is null, Base16FormatProvider.Invariant (lowercase hex) is used.

Parameter Type Description
value string The base-encoded string to parse.
provider IFormatProvider? The format provider that selects the base encoding, or null to use Base16FormatProvider.Invariant.

Returns — A new DigitalSignature constructed from the decoded bytes.

Exceptions

Exception Condition
FormatException value is not valid for the specified encoding.

Parse(ReadOnlySpan<char>, IFormatProvider?)

public static DigitalSignature Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)

Parses a base-encoded character span into a DigitalSignature. When provider is null, Base16FormatProvider.Invariant is used.

Parameter Type Description
value ReadOnlySpan<char> The base-encoded character span to parse.
provider IFormatProvider? The format provider that selects the base encoding, or null to use Base16FormatProvider.Invariant.

Returns — A new DigitalSignature constructed from the decoded bytes.

Exceptions

Exception Condition
FormatException value is not valid for the specified encoding.

TryParse(string?, IFormatProvider?, out DigitalSignature)

public static bool TryParse(string? value, IFormatProvider? provider, out DigitalSignature result)

Tries to parse a base-encoded string into a DigitalSignature. When provider is null, Base16FormatProvider.Invariant is used.

Parameter Type Description
value string? The base-encoded string to parse.
provider IFormatProvider? The format provider that selects the base encoding, or null to use Base16FormatProvider.Invariant.
result out DigitalSignature On return, contains the parsed value, or default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.


TryParse(ReadOnlySpan<char>, IFormatProvider?, out DigitalSignature)

public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out DigitalSignature result)

Tries to parse a base-encoded character span into a DigitalSignature. When provider is null, Base16FormatProvider.Invariant is used.

Parameter Type Description
value ReadOnlySpan<char> The base-encoded character span to parse.
provider IFormatProvider? The format provider that selects the base encoding, or null to use Base16FormatProvider.Invariant.
result out DigitalSignature On return, contains the parsed value, or default if parsing failed.

Returns — true if parsing succeeded; otherwise, false.

Instance 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(DigitalSignature) bool Determines whether this instance equals another DigitalSignature 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 signature bytes as a ReadOnlyMemory<byte>. Satisfies IBinaryConvertible.

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


AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

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

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


Equals(DigitalSignature)

public bool Equals(DigitalSignature other)

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

Parameter Type Description
other DigitalSignature 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 DigitalSignature 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 signature bytes.


ToString(IFormatProvider)

public string ToString(IFormatProvider provider)

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

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

Returns — A base-encoded string representation of the signature bytes.

Implicit Conversion Operators

Operator Description
implicit operator DigitalSignature(ReadOnlySpan<byte>) Creates a DigitalSignature from a ReadOnlySpan<byte>.
implicit operator DigitalSignature(ReadOnlyMemory<byte>) Creates a DigitalSignature from a ReadOnlyMemory<byte>.
implicit operator DigitalSignature(ReadOnlySequence<byte>) Creates a DigitalSignature from a ReadOnlySequence<byte>.

implicit operator DigitalSignature(ReadOnlySpan<byte>)

public static implicit operator DigitalSignature(ReadOnlySpan<byte> value)

Creates a new DigitalSignature from the specified ReadOnlySpan<byte>.

implicit operator DigitalSignature(ReadOnlyMemory<byte>)

public static implicit operator DigitalSignature(ReadOnlyMemory<byte> value)

Creates a new DigitalSignature from the specified ReadOnlyMemory<byte>.

implicit operator DigitalSignature(ReadOnlySequence<byte>)

public static implicit operator DigitalSignature(ReadOnlySequence<byte> value)

Creates a new DigitalSignature from the specified ReadOnlySequence<byte>.

Operators

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

operator ==

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

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

operator !=

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

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

See Also

ONIXLabs .NET Library


Clone this wiki locally