Table of Contents

Class HmacCore

Namespace
CryptoHives.Foundation.Security.Cryptography.Mac
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

Implements HMAC (Hash-based Message Authentication Code) as defined in RFC 2104.

public class HmacCore : IMac, IDisposable
Inheritance
HmacCore
Implements
Derived
Inherited Members

Remarks

HMAC uses an underlying hash function to produce a keyed authentication tag. The construction is: HMAC(K, m) = H((K' ⊕ opad) ‖ H((K' ⊕ ipad) ‖ m)), where K' is the key padded or hashed to the hash block size.

This is a fully managed implementation that works with any HashAlgorithm from the CryptoHives library, providing deterministic behavior across all platforms.

Constructors

HmacCore(string, HashAlgorithm, HashAlgorithm, ReadOnlySpan<byte>)

Initializes a new instance of the HmacCore class.

public HmacCore(string algorithmName, HashAlgorithm innerHash, HashAlgorithm outerHash, ReadOnlySpan<byte> key)

Parameters

algorithmName string

The name of the HMAC algorithm (e.g. "HMAC-SHA256").

innerHash HashAlgorithm

The hash algorithm instance for the inner hash.

outerHash HashAlgorithm

The hash algorithm instance for the outer hash.

key ReadOnlySpan<byte>

The secret key.

Exceptions

ArgumentNullException

innerHash, outerHash, or key is null.

Properties

AlgorithmName

Gets the name of the MAC algorithm.

public string AlgorithmName { get; }

Property Value

string

MacSize

Gets the MAC output size in bytes.

public int MacSize { get; }

Property Value

int

Methods

ComputeHash(byte[])

Computes the HMAC tag for the given data in a single operation.

public byte[] ComputeHash(byte[] data)

Parameters

data byte[]

The data to authenticate.

Returns

byte[]

The MAC tag.

ComputeHash(ReadOnlySpan<byte>)

Computes the HMAC tag for the given data in a single operation.

public byte[] ComputeHash(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The data to authenticate.

Returns

byte[]

The MAC tag.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

Releases the resources used by this instance.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

True if called from Dispose.

Finalize(Span<byte>)

Computes the final MAC tag and writes it to destination.

public void Finalize(Span<byte> destination)

Parameters

destination Span<byte>

The buffer to receive the MAC tag. Must be at least MacSize bytes.

Exceptions

ArgumentException

destination is too small.

ObjectDisposedException

Thrown when the instance has been disposed.

Reset()

Resets the MAC to its initial state so it can be reused with the same key.

public void Reset()

Update(ReadOnlySpan<byte>)

Feeds input data into the MAC computation.

public void Update(ReadOnlySpan<byte> input)

Parameters

input ReadOnlySpan<byte>

The data to process.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.