Skip to content

sgns::neoswarm::security::MessageSigning

Signs and verifies inter-node message payloads.

#include <message_signing.hpp>

Public Functions

Name
MessageSigning(const NodeIdentity & identity)
Construct with a reference to the local node identity.
outcome::result< std::vector< uint8_t > > Sign(const std::string & payload) const
Sign a serialised message payload.
std::string AttachSignature(const std::string & payload) const
Attach a signature field to a JSON payload string.
bool Verify(const std::string & payload, const std::vector< uint8_t > & signature, const std::string & m_pubKeyhex)
Verify a signature against a known public key.
std::string GenerateNonce()
Generate a cryptographically random nonce.
uint64_t CurrentTimestampMs()
Get current Unix timestamp in milliseconds.
bool VerifyAndStrip(std::string & payload, const std::string & m_pubKeyhex)
Verify and strip the signature field from a signed JSON payload.

Public Attributes

Name
constexpr int64_t kReplayWindowSec
Replay protection window in seconds.

Public Functions Documentation

function MessageSigning

explicit MessageSigning(
    const NodeIdentity & identity
)

Construct with a reference to the local node identity.

Parameters:

  • identity Node identity used for signing.

function Sign

outcome::result< std::vector< uint8_t > > Sign(
    const std::string & payload
) const

Sign a serialised message payload.

Parameters:

  • payload UTF-8 payload string.

Return: DER-encoded signature bytes or IdentityError.

function AttachSignature

std::string AttachSignature(
    const std::string & payload
) const

Attach a signature field to a JSON payload string.

Parameters:

  • payload JSON object string (must end with '}').

Return: Payload with appended "sig" field.

function Verify

static bool Verify(
    const std::string & payload,
    const std::vector< uint8_t > & signature,
    const std::string & m_pubKeyhex
)

Verify a signature against a known public key.

Parameters:

  • payload Original payload string.
  • signature DER-encoded signature bytes.
  • m_pubKeyhex Hex-encoded compressed public key of the signer.

Return: True if the signature is valid.

function GenerateNonce

static std::string GenerateNonce()

Generate a cryptographically random nonce.

Return: Hex-encoded 32-byte nonce.

function CurrentTimestampMs

static uint64_t CurrentTimestampMs()

Get current Unix timestamp in milliseconds.

Return: Milliseconds since epoch.

function VerifyAndStrip

static bool VerifyAndStrip(
    std::string & payload,
    const std::string & m_pubKeyhex
)

Verify and strip the signature field from a signed JSON payload.

Parameters:

  • payload On entry: signed JSON. On exit: payload without sig.
  • m_pubKeyhex Hex-encoded public key of the expected signer.

Return: True if the signature is valid.

Public Attributes Documentation

variable kReplayWindowSec

static constexpr int64_t kReplayWindowSec = 30;

Replay protection window in seconds.


Updated on 2026-07-25 at 22:56:56 +0000