Class CryptoAuth
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Provides methods for message authentication using the crypto_auth API from libsodium.
[Obsolete("Use CryptoHmacSha512_256 instead. CryptoAuth is deprecated and will be removed in future versions.")]
public static class CryptoAuth
- Inheritance
-
CryptoAuth
- Inherited Members
Remarks
This class wraps the crypto_auth
functions from libsodium's secret-key authentication API,
which uses the HMAC-SHA-512-256 algorithm to generate and verify message authentication codes (MACs).
Fields
KeyLen
The length, in bytes, of a valid secret key for HMAC-SHA-512-256.
public const int KeyLen = 32
Field Value
MacLen
The length, in bytes, of the MAC produced by HMAC-SHA-512-256.
public const int MacLen = 32
Field Value
Methods
ComputeMac(Span<byte>, ReadOnlySpan<byte>, SecureMemory<byte>)
Computes a message authentication code (MAC) for the given input using HMAC-SHA-512-256.
public static void ComputeMac(Span<byte> mac, ReadOnlySpan<byte> input, SecureMemory<byte> key)
Parameters
mac
Span<byte>A writable buffer with a length of MacLen bytes that will receive the computed MAC.
input
ReadOnlySpan<byte>The message data to authenticate.
key
SecureMemory<byte>The secret key to use for authentication, must be KeyLen bytes long.
Remarks
This method is a wrapper around libsodium's crypto_auth
function.
Exceptions
- ArgumentException
Thrown when the length of
mac
orkey
is invalid.- LibSodiumException
Thrown when the underlying libsodium operation fails unexpectedly.
ComputeMac(Span<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Computes a message authentication code (MAC) for the given input using HMAC-SHA-512-256.
public static void ComputeMac(Span<byte> mac, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key)
Parameters
mac
Span<byte>A writable buffer with a length of MacLen bytes that will receive the computed MAC.
input
ReadOnlySpan<byte>The message data to authenticate.
key
ReadOnlySpan<byte>The secret key to use for authentication, must be KeyLen bytes long.
Remarks
This method is a wrapper around libsodium's crypto_auth
function.
Exceptions
- ArgumentException
Thrown when the length of
mac
orkey
is invalid.- LibSodiumException
Thrown when the underlying libsodium operation fails unexpectedly.
GenerateKey(Span<byte>)
Generates a new random secret key for use with HMAC-SHA-512-256.
public static void GenerateKey(Span<byte> key)
Parameters
key
Span<byte>A writable buffer with a length of KeyLen bytes that will be filled with the generated key.
Remarks
This function uses a cryptographically secure random number generator to produce a secret key.
Internally calls crypto_auth_keygen
from libsodium.
Exceptions
- ArgumentException
Thrown when the buffer length is not equal to KeyLen.
TryVerifyMac(ReadOnlySpan<byte>, ReadOnlySpan<byte>, SecureMemory<byte>)
Attempts to verify that a given MAC is valid for the specified input and key using HMAC-SHA-512-256.
public static bool TryVerifyMac(ReadOnlySpan<byte> mac, ReadOnlySpan<byte> input, SecureMemory<byte> key)
Parameters
mac
ReadOnlySpan<byte>The message authentication code to verify. Must be MacLen bytes long.
input
ReadOnlySpan<byte>The original message data that the MAC should authenticate.
key
SecureMemory<byte>The secret key that was used to generate the MAC. Must be KeyLen bytes long.
Returns
- bool
true
if the MAC is valid; otherwise,false
.
Remarks
This method wraps the crypto_auth_verify
function from libsodium.
Exceptions
- ArgumentException
Thrown when the length of
mac
orkey
is invalid.- LibSodiumException
Thrown when an unexpected error occurs during verification.
TryVerifyMac(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Attempts to verify that a given MAC is valid for the specified input and key using HMAC-SHA-512-256.
public static bool TryVerifyMac(ReadOnlySpan<byte> mac, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key)
Parameters
mac
ReadOnlySpan<byte>The message authentication code to verify. Must be MacLen bytes long.
input
ReadOnlySpan<byte>The original message data that the MAC should authenticate.
key
ReadOnlySpan<byte>The secret key that was used to generate the MAC. Must be KeyLen bytes long.
Returns
- bool
true
if the MAC is valid; otherwise,false
.
Remarks
This method wraps the crypto_auth_verify
function from libsodium.
Exceptions
- ArgumentException
Thrown when the length of
mac
orkey
is invalid.- LibSodiumException
Thrown when an unexpected error occurs during verification.
VerifyMac(ReadOnlySpan<byte>, ReadOnlySpan<byte>, SecureMemory<byte>)
Verifies that a given MAC is valid for the specified input and key using HMAC-SHA-512-256.
public static void VerifyMac(ReadOnlySpan<byte> mac, ReadOnlySpan<byte> input, SecureMemory<byte> key)
Parameters
mac
ReadOnlySpan<byte>The message authentication code to verify. Must be MacLen bytes long.
input
ReadOnlySpan<byte>The original message data that the MAC should authenticate.
key
SecureMemory<byte>The secret key that was used to generate the MAC. Must be KeyLen bytes long.
Exceptions
- ArgumentException
Thrown when the length of
mac
orkey
is invalid.- LibSodiumException
Thrown when the MAC verification fails or an unexpected error occurs.
VerifyMac(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Verifies that a given MAC is valid for the specified input and key using HMAC-SHA-512-256.
public static void VerifyMac(ReadOnlySpan<byte> mac, ReadOnlySpan<byte> input, ReadOnlySpan<byte> key)
Parameters
mac
ReadOnlySpan<byte>The message authentication code to verify. Must be MacLen bytes long.
input
ReadOnlySpan<byte>The original message data that the MAC should authenticate.
key
ReadOnlySpan<byte>The secret key that was used to generate the MAC. Must be KeyLen bytes long.
Exceptions
- ArgumentException
Thrown when the length of
mac
orkey
is invalid.- LibSodiumException
Thrown when the MAC verification fails or an unexpected error occurs.