Repository navigation
OnixLabs.Security.Cryptography.DigitalSignature
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>
|
public readonly partial struct DigitalSignature :
ICryptoPrimitive<DigitalSignature>,
ISpanParsable<DigitalSignature>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 anullinternal array. Comparisons involving a default instance are safe; two default instances are considered equal.
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); // Trueusing OnixLabs.Security.Cryptography;
byte[] raw = GetSignatureBytesFromStorage();
DigitalSignature signature = new DigitalSignature(raw.AsSpan());using OnixLabs.Security.Cryptography;
ReadOnlyMemory<byte> memory = GetSignatureMemory();
DigitalSignature signature = memory; // implicit operatorusing OnixLabs.Core.Text;
using OnixLabs.Security.Cryptography;
DigitalSignature signature = GetSignature();
string base64 = signature.ToString(Base64FormatProvider.Invariant);| 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>. |
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. |
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. |
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. |
| 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. |
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. |
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. |
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.
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.
| 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. |
public ReadOnlyMemory<byte> AsReadOnlyMemory()Returns the underlying signature bytes as a ReadOnlyMemory<byte>. Satisfies IBinaryConvertible.
Returns — A ReadOnlyMemory<byte> over the underlying byte array.
public ReadOnlySpan<byte> AsReadOnlySpan()Returns the underlying signature bytes as a ReadOnlySpan<byte>. Satisfies IBinaryConvertible.
Returns — A ReadOnlySpan<byte> over the underlying byte array.
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.
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.
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 signature bytes.
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.
| 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>. |
public static implicit operator DigitalSignature(ReadOnlySpan<byte> value)Creates a new DigitalSignature from the specified ReadOnlySpan<byte>.
public static implicit operator DigitalSignature(ReadOnlyMemory<byte> value)Creates a new DigitalSignature from the specified ReadOnlyMemory<byte>.
public static implicit operator DigitalSignature(ReadOnlySequence<byte> value)Creates a new DigitalSignature from the specified ReadOnlySequence<byte>.
| Operator | Description |
|---|---|
operator == |
Determines whether two DigitalSignature values are equal. |
operator != |
Determines whether two DigitalSignature values are not equal. |
public static bool operator ==(DigitalSignature left, DigitalSignature right)Determines whether two DigitalSignature values are equal. Delegates to Equals(DigitalSignature), which uses constant-time comparison.
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.
- OnixLabs.Security.Cryptography
- Home
- OnixLabs.Security.Cryptography.DigitalSignatureAndPublicKey
- OnixLabs.Security.Cryptography.Salt
- OnixLabs.Security.Cryptography.Secret
- OnixLabs.Security.Cryptography.Hash
- OnixLabs.Security.Cryptography.NamedPublicKey
- IValueEquatable
- OnixLabs.Security.Cryptography on NuGet
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