Table of Contents

Class SignedMessageExtensions

Namespace
Savvyio.Messaging.Cryptography
Assembly
Savvyio.Messaging.dll

Extension methods for the ISignedMessage<T> interface.

public static class SignedMessageExtensions
Inheritance
SignedMessageExtensions

Examples

This example shows how to verify a signed message before a consumer trusts the request payload.

using System;
using Savvyio;
using Savvyio.Messaging;
using Savvyio.Messaging.Cryptography;

namespace ExampleApp;

using System;
using System.IO;
using System.Text;
using Savvyio;

public sealed class DemoMarshaller : IMarshaller
{
    public Stream Serialize<TValue>(TValue value) => new MemoryStream(Encoding.UTF8.GetBytes(value?.ToString() ?? string.Empty));
    public Stream Serialize(object value, Type inputType) => Serialize(value?.ToString() ?? string.Empty);
    public TValue Deserialize<TValue>(Stream data) => throw new NotSupportedException();
    public object Deserialize(Stream data, Type returnType) => throw new NotSupportedException();
}

public sealed class SignedMessageExtensionsExample
{
    public void Verify()
    {
        var message = new Message<CreateOrderCommand>("msg-42", new Uri("urn:orders"), "orders.created", new CreateOrderCommand("ORD-42"));
        var signed = message.Sign(new DemoMarshaller(), options => options.SignatureSecret = new byte[] { 1, 2, 3 });
        signed.CheckSignature(new DemoMarshaller(), options => options.SignatureSecret = new byte[] { 1, 2, 3 });
    }
}

public sealed record CreateOrderCommand(string OrderId) : Request;

Methods

CheckSignature<T>(ISignedMessage<T>, IMarshaller, Action<SignedMessageOptions>)

Verifies the digital signature of the ISignedMessage<T> message.

public static void CheckSignature<T>(this ISignedMessage<T> message, IMarshaller marshaller, Action<SignedMessageOptions> setup) where T : IRequest

Parameters

message ISignedMessage<T>

The IRequest message to cryptographically verify.

marshaller IMarshaller

The IMarshaller that is used when converting the message into an ISignedMessage<T>.

setup Action<SignedMessageOptions>

The SignedMessageOptions that must be configured.

Type Parameters

T

The type of the payload constraint to the IRequest interface.

Remarks

This method throws an ArgumentOutOfRangeException if the verification of the digital signature fails.

Exceptions

ArgumentNullException

message cannot be null -or- marshaller cannot be null.

ArgumentException

setup failed to configure an instance of SignedMessageOptions in a valid state.

ArgumentOutOfRangeException

message signature did not match the cryptographically calculated value.