Table of Contents

Class CryptoOneTimeAuth

Namespace
LibSodium
Assembly
LibSodium.Net.dll

Computes and verifies Poly1305 one-time authentication codes.

public static class CryptoOneTimeAuth
Inheritance
CryptoOneTimeAuth
Inherited Members

Remarks

Based on libsodium's crypto_onetimeauth API: https://doc.libsodium.org/advanced/poly1305

Fields

KeyLen

Length of the secret key in bytes (32).

public static readonly int KeyLen

Field Value

int

MacLen

Length of the MAC output in bytes (16).

public static readonly int MacLen

Field Value

int

Methods

ComputeMac(SecureMemory<byte>, Stream, Span<byte>)

Computes a Poly1305 authentication tag from a stream.

public static void ComputeMac(SecureMemory<byte> key, Stream messageStream, Span<byte> mac)

Parameters

key SecureMemory<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac Span<byte>

A buffer to receive the 16-byte MAC.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

LibSodiumException

Thrown if the MAC computation fails internally.

ComputeMac(SecureMemory<byte>, ReadOnlySpan<byte>, Span<byte>)

Computes a Poly1305 authentication tag for the given message.

public static int ComputeMac(SecureMemory<byte> key, ReadOnlySpan<byte> message, Span<byte> mac)

Parameters

key SecureMemory<byte>

A 32-byte secret key.

message ReadOnlySpan<byte>

The message to authenticate.

mac Span<byte>

A buffer to receive the 16-byte MAC.

Returns

int

The number of bytes written to mac (always 16).

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

LibSodiumException

Thrown if the MAC computation fails internally.

ComputeMac(ReadOnlySpan<byte>, Stream, Span<byte>)

Computes a Poly1305 authentication tag from a stream.

public static void ComputeMac(ReadOnlySpan<byte> key, Stream messageStream, Span<byte> mac)

Parameters

key ReadOnlySpan<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac Span<byte>

A buffer to receive the 16-byte MAC.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

LibSodiumException

Thrown if the MAC computation fails internally.

ComputeMac(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)

Computes a Poly1305 authentication tag for the given message.

public static int ComputeMac(ReadOnlySpan<byte> key, ReadOnlySpan<byte> message, Span<byte> mac)

Parameters

key ReadOnlySpan<byte>

A 32-byte secret key.

message ReadOnlySpan<byte>

The message to authenticate.

mac Span<byte>

A buffer to receive the 16-byte MAC.

Returns

int

The number of bytes written to mac (always 16).

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

LibSodiumException

Thrown if the MAC computation fails internally.

ComputeMacAsync(SecureMemory<byte>, Stream, Memory<byte>, CancellationToken)

Asynchronously computes a Poly1305 authentication tag from a stream.

public static Task ComputeMacAsync(SecureMemory<byte> key, Stream messageStream, Memory<byte> mac, CancellationToken cancellationToken = default)

Parameters

key SecureMemory<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac Memory<byte>

A buffer to receive the 16-byte MAC.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task

A task that represents the asynchronous operation.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

LibSodiumException

Thrown if the MAC computation fails internally.

ComputeMacAsync(ReadOnlyMemory<byte>, Stream, Memory<byte>, CancellationToken)

Asynchronously computes a Poly1305 authentication tag from a stream.

public static Task ComputeMacAsync(ReadOnlyMemory<byte> key, Stream messageStream, Memory<byte> mac, CancellationToken cancellationToken = default)

Parameters

key ReadOnlyMemory<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac Memory<byte>

A buffer to receive the 16-byte MAC.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task

A task that represents the asynchronous operation.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

LibSodiumException

Thrown if the MAC computation fails internally.

CreateIncrementalMac(SecureMemory<byte>)

Creates an incremental hash object using the Poly1305 algorithm.

public static ICryptoIncrementalOperation CreateIncrementalMac(SecureMemory<byte> key)

Parameters

key SecureMemory<byte>

The cryptographic key (32 bytes) to use for the Poly1305 computation.

Returns

ICryptoIncrementalOperation

An ICryptoIncrementalOperation instance that allows incremental computation of the Poly1305 hash.

Remarks

The returned ICryptoIncrementalOperation can be used to compute the Poly1305 hash incrementally by processing data in chunks.

CreateIncrementalMac(ReadOnlySpan<byte>)

Creates an incremental hash object using the Poly1305 algorithm.

public static ICryptoIncrementalOperation CreateIncrementalMac(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

The cryptographic key (32 bytes) to use for the Poly1305 computation.

Returns

ICryptoIncrementalOperation

An ICryptoIncrementalOperation instance that allows incremental computation of the Poly1305 hash.

Remarks

The returned ICryptoIncrementalOperation can be used to compute the Poly1305 hash incrementally by processing data in chunks.

GenerateKey(SecureMemory<byte>)

Generates a random 32-byte key suitable for Poly1305.

public static void GenerateKey(SecureMemory<byte> key)

Parameters

key SecureMemory<byte>

A buffer to receive the generated key (must be 32 bytes).

GenerateKey(Span<byte>)

Generates a random 32-byte key suitable for Poly1305.

public static void GenerateKey(Span<byte> key)

Parameters

key Span<byte>

A buffer to receive the generated key (must be 32 bytes).

VerifyMac(SecureMemory<byte>, Stream, ReadOnlySpan<byte>)

Verifies a Poly1305 authentication tag from a stream.

public static bool VerifyMac(SecureMemory<byte> key, Stream messageStream, ReadOnlySpan<byte> mac)

Parameters

key SecureMemory<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac ReadOnlySpan<byte>

The expected 16-byte MAC.

Returns

bool

true if the MAC is valid; otherwise, false.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

VerifyMac(SecureMemory<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Verifies a Poly1305 authentication tag against a given message.

public static bool VerifyMac(SecureMemory<byte> key, ReadOnlySpan<byte> message, ReadOnlySpan<byte> mac)

Parameters

key SecureMemory<byte>

A 32-byte secret key.

message ReadOnlySpan<byte>

The message to verify.

mac ReadOnlySpan<byte>

The expected 16-byte MAC.

Returns

bool

true if the MAC is valid; otherwise, false.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

VerifyMac(ReadOnlySpan<byte>, Stream, ReadOnlySpan<byte>)

Verifies a Poly1305 authentication tag from a stream.

public static bool VerifyMac(ReadOnlySpan<byte> key, Stream messageStream, ReadOnlySpan<byte> mac)

Parameters

key ReadOnlySpan<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac ReadOnlySpan<byte>

The expected 16-byte MAC.

Returns

bool

true if the MAC is valid; otherwise, false.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

VerifyMac(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Verifies a Poly1305 authentication tag against a given message.

public static bool VerifyMac(ReadOnlySpan<byte> key, ReadOnlySpan<byte> message, ReadOnlySpan<byte> mac)

Parameters

key ReadOnlySpan<byte>

A 32-byte secret key.

message ReadOnlySpan<byte>

The message to verify.

mac ReadOnlySpan<byte>

The expected 16-byte MAC.

Returns

bool

true if the MAC is valid; otherwise, false.

Exceptions

ArgumentException

Thrown if key or mac has an invalid length.

VerifyMacAsync(SecureMemory<byte>, Stream, ReadOnlyMemory<byte>, CancellationToken)

Asynchronously verifies a Poly1305 authentication tag from a stream.

public static Task<bool> VerifyMacAsync(SecureMemory<byte> key, Stream messageStream, ReadOnlyMemory<byte> mac, CancellationToken cancellationToken = default)

Parameters

key SecureMemory<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac ReadOnlyMemory<byte>

The expected 16-byte MAC.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<bool>

true if the MAC is valid; otherwise, false.

VerifyMacAsync(ReadOnlyMemory<byte>, Stream, ReadOnlyMemory<byte>, CancellationToken)

Asynchronously verifies a Poly1305 authentication tag from a stream.

public static Task<bool> VerifyMacAsync(ReadOnlyMemory<byte> key, Stream messageStream, ReadOnlyMemory<byte> mac, CancellationToken cancellationToken = default)

Parameters

key ReadOnlyMemory<byte>

A 32-byte secret key.

messageStream Stream

A stream containing the message.

mac ReadOnlyMemory<byte>

The expected 16-byte MAC.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

Task<bool>

true if the MAC is valid; otherwise, false.