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
MacLen
Length of the MAC output in bytes (16).
public static readonly int MacLen
Field Value
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
keySecureMemory<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macSpan<byte>A buffer to receive the 16-byte MAC.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keySecureMemory<byte>A 32-byte secret key.
messageReadOnlySpan<byte>The message to authenticate.
macSpan<byte>A buffer to receive the 16-byte MAC.
Returns
- int
The number of bytes written to
mac(always 16).
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keyReadOnlySpan<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macSpan<byte>A buffer to receive the 16-byte MAC.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keyReadOnlySpan<byte>A 32-byte secret key.
messageReadOnlySpan<byte>The message to authenticate.
macSpan<byte>A buffer to receive the 16-byte MAC.
Returns
- int
The number of bytes written to
mac(always 16).
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keySecureMemory<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macMemory<byte>A buffer to receive the 16-byte MAC.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keyReadOnlyMemory<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macMemory<byte>A buffer to receive the 16-byte MAC.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keySecureMemory<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
keyReadOnlySpan<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
keySecureMemory<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
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
keySecureMemory<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macReadOnlySpan<byte>The expected 16-byte MAC.
Returns
- bool
trueif the MAC is valid; otherwise,false.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keySecureMemory<byte>A 32-byte secret key.
messageReadOnlySpan<byte>The message to verify.
macReadOnlySpan<byte>The expected 16-byte MAC.
Returns
- bool
trueif the MAC is valid; otherwise,false.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keyReadOnlySpan<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macReadOnlySpan<byte>The expected 16-byte MAC.
Returns
- bool
trueif the MAC is valid; otherwise,false.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keyReadOnlySpan<byte>A 32-byte secret key.
messageReadOnlySpan<byte>The message to verify.
macReadOnlySpan<byte>The expected 16-byte MAC.
Returns
- bool
trueif the MAC is valid; otherwise,false.
Exceptions
- ArgumentException
Thrown if
keyormachas 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
keySecureMemory<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macReadOnlyMemory<byte>The expected 16-byte MAC.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
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
keyReadOnlyMemory<byte>A 32-byte secret key.
messageStreamStreamA stream containing the message.
macReadOnlyMemory<byte>The expected 16-byte MAC.
cancellationTokenCancellationTokenA token to cancel the operation.