Table of Contents

SignatureValue Struct

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
SignatureValue.cs

Represents a digital-signature value together with its wire encoding.

public readonly struct SignatureValue : IEquatable<SignatureValue>
Implements
Inherited Members
Extension Methods

Examples

// Record a signature together with the wire encoding its producer emitted.
SignatureValue signature = SignatureValue.FromBytes(signatureBytes, SignatureFormat.P1363);

// Downstream code branches on the recorded format instead of guessing, avoiding the classic
// failure of feeding a fixed-width P1363 signature to a verifier that expects ASN.1 DER.
ReadOnlySpan<byte> bytes = signature.AsSpan();
bool valid = signature.Format == SignatureFormat.Der
    ? VerifyDer(bytes)
    : VerifyP1363(bytes);

Remarks

A signature is a byte sequence whose interpretation depends on its encoding (see SignatureFormat). Carrying the Format with the bytes prevents the most common signature interoperability failure: passing an ASN.1 DER signature to a verifier expecting the fixed-width IEEE P1363 form, or vice versa.

SignatureValue carries its bytes by defensive copy, and the default instance ( default(SignatureValue)) is the empty value with Unknown: Length is 0 and IsEmpty is true.

Equals(SignatureValue) compares both the format and the bytes; FixedTimeEquals(SignatureValue) compares the bytes only, in fixed time.

Properties

Format

Gets the wire encoding of the signature bytes.

public SignatureFormat Format { get; }

Property Value

SignatureFormat

The SignatureFormat recorded when the value was created; Unknown for the default instance.

IsEmpty

Gets a value indicating whether the signature is empty.

public bool IsEmpty { get; }

Property Value

bool

true if the signature contains no bytes; otherwise, false.

Length

Gets the number of bytes in the signature.

public int Length { get; }

Property Value

int

The signature length in bytes, or 0 for the empty value.

Methods

AsSpan()

Returns a read-only view over the signature bytes.

public ReadOnlySpan<byte> AsSpan()

Returns

ReadOnlySpan<byte>

A read-only span over the value; empty for the empty value.

Equals(SignatureValue)

Determines whether this signature equals another, comparing both format and bytes.

public bool Equals(SignatureValue other)

Parameters

other SignatureValue

The signature to compare against.

Returns

bool

true if both values record the same Format and contain identical bytes; otherwise, false.

Remarks

After the format check, the byte comparison is constant-time in content - it runs through FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>), so its duration depends only on the operand length, not on where the bytes first differ. A format or length mismatch returns false immediately (neither is secret). FixedTimeEquals(SignatureValue) remains available and behaves identically.

Equals(object?)

Determines whether this signature equals the specified object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare against.

Returns

bool

true if obj is a SignatureValue with the same format and identical bytes; otherwise, false.

FixedTimeEquals(SignatureValue)

Compares the bytes of this signature to another in fixed time, ignoring Format.

public bool FixedTimeEquals(SignatureValue other)

Parameters

other SignatureValue

The signature to compare against.

Returns

bool

true if both signatures have the same length and identical bytes; otherwise, false.

Remarks

Built on FixedTimeEquals(ReadOnlySpan<byte>, ReadOnlySpan<byte>): the comparison time depends only on the length of the operands, not on their content. A length mismatch returns false immediately, so the lengths themselves are not concealed. The recorded format is intentionally excluded; compare Format separately when the encoding matters.

FromBytes(ReadOnlySpan<byte>, SignatureFormat)

Creates a SignatureValue from the provided bytes and encoding.

public static SignatureValue FromBytes(ReadOnlySpan<byte> value, SignatureFormat format)

Parameters

value ReadOnlySpan<byte>

The signature bytes to copy. An empty span yields an empty value carrying format.

format SignatureFormat

The wire encoding of value.

Returns

SignatureValue

A new SignatureValue containing a defensive copy of value.

Remarks

The structural validity of value against format is not verified; the format is metadata recorded by the producer.

Exceptions

ArgumentOutOfRangeException

format is not a defined SignatureFormat value.

GetHashCode()

Returns a hash code computed over the format and the signature bytes.

public override int GetHashCode()

Returns

int

A hash code consistent with Equals(SignatureValue).

ToArray()

Copies the signature bytes into a new array.

public byte[] ToArray()

Returns

byte[]

A new array containing the signature bytes; an empty array for the empty value.

ToBase64String()

Formats the signature as a Base64 string.

public string ToBase64String()

Returns

string

The Base64 representation, or Empty for the empty value.

ToHexString()

Formats the signature as a lowercase hexadecimal string.

public string ToHexString()

Returns

string

The lowercase hexadecimal representation, or Empty for the empty value.

ToString()

Returns the lowercase hexadecimal representation of the signature bytes.

public override string ToString()

Returns

string

The same text as ToHexString().

Remarks

Signatures are not secrets, so the content is intentionally included in the string representation. The recorded format is not part of the text; read Format directly when it is needed.

Operators

operator ==(SignatureValue, SignatureValue)

Determines whether two signatures are equal.

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

Parameters

left SignatureValue

The first signature.

right SignatureValue

The second signature.

Returns

bool

true if the values record the same format and contain identical bytes; otherwise, false.

operator !=(SignatureValue, SignatureValue)

Determines whether two signatures are not equal.

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

Parameters

left SignatureValue

The first signature.

right SignatureValue

The second signature.

Returns

bool

true if the values differ in format or bytes; otherwise, false.

Applies to

ProductVersions
.NET8, 10