MacAlgorithm Class

Represents a message authentication code (MAC) algorithm.

public abstract class MacAlgorithm : Algorithm

Inheritance Hierarchy

Static Properties

Blake2b_128

Gets the BLAKE2b algorithm with a 256-bit key size and a 128-bit output size.

public static Blake2bMac Blake2b_128 { get; }

Blake2b_256

Gets the BLAKE2b algorithm with a 256-bit key size and a 256-bit output size.

public static Blake2bMac Blake2b_256 { get; }

Blake2b_512

Gets the BLAKE2b algorithm with a 256-bit key size and a 512-bit output size.

public static Blake2bMac Blake2b_512 { get; }

HmacSha256

Gets the HMAC-SHA256 algorithm.

public static HmacSha256 HmacSha256 { get; }

HmacSha512

Gets the HMAC-SHA512 algorithm.

public static HmacSha512 HmacSha512 { get; }

Properties

KeySize

Gets the size of keys.

public int KeySize { get; }

Property Value

The key size, in bytes.

MacSize

Gets the size of a MAC.

public int MacSize { get; }

Property Value

The MAC size, in bytes.

Methods

Mac(Key, ReadOnlySpan<byte>)

Computes a message authentication code for the specified input data using the specified key and returns it as an array of bytes.

public byte[] Mac(
    Key key,
    ReadOnlySpan<byte> data)

Parameters

key
The key to use for computing the message authentication code.
data
The data to authenticate.

Return Value

The computed message authentication code.

Exceptions

ArgumentNullException
key is null.
ArgumentException
key.Algorithm is not the same object as the current MacAlgorithm object.
ObjectDisposedException
key has been disposed.

Mac(Key, ReadOnlySpan<byte>, Span<byte>)

Fills the specified span of bytes with a message authentication code for the specified input data using the specified key.

public void Mac(
    Key key,
    ReadOnlySpan<byte> data,
    Span<byte> mac)

Parameters

key
The key to use for computing the message authentication code.
data
The data to authenticate.
mac
The span to fill with the computed message authentication code.

Exceptions

ArgumentNullException
key is null.
ArgumentException
key.Algorithm is not the same object as the current MacAlgorithm object.
ArgumentException
mac.Length is not equal to MacSize.
ObjectDisposedException
key has been disposed.

Verify(Key, ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Verifies the specified input data using the specified key and message authentication code.

public bool Verify(
    Key key,
    ReadOnlySpan<byte> data,
    ReadOnlySpan<byte> mac)

Parameters

key
The key to use for verification. Verification fails if this is not the key used when computing the message authentication code.
data
The data to verify. Verification fails if the integrity of the data was compromised.
mac
The message authentication code for the data.

Return Value

true if verification succeeds; otherwise, false.

Exceptions

ArgumentNullException
key is null.
ArgumentException
key.Algorithm is not the same object as the current MacAlgorithm object.
ObjectDisposedException
key has been disposed.

Thread Safety

All members of this type are thread safe.

Purity

All methods yield the same result for the same arguments.

See Also