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
Length
Gets the number of bytes in the signature.
public int Length { get; }
Property Value
- int
The signature length in bytes, or
0for 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
otherSignatureValueThe signature to compare against.
Returns
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
objobjectThe object to compare against.
Returns
- bool
true if
objis 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
otherSignatureValueThe signature to compare against.
Returns
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
valueReadOnlySpan<byte>The signature bytes to copy. An empty span yields an empty value carrying
format.formatSignatureFormatThe 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
formatis 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
ToHexString()
Formats the signature as a lowercase hexadecimal string.
public string ToHexString()
Returns
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
leftSignatureValueThe first signature.
rightSignatureValueThe second signature.
Returns
operator !=(SignatureValue, SignatureValue)
Determines whether two signatures are not equal.
public static bool operator !=(SignatureValue left, SignatureValue right)
Parameters
leftSignatureValueThe first signature.
rightSignatureValueThe second signature.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |