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
algorithmNamestringThe name of the HMAC algorithm (e.g. "HMAC-SHA256").
innerHashHashAlgorithmThe hash algorithm instance for the inner hash.
outerHashHashAlgorithmThe hash algorithm instance for the outer hash.
keyReadOnlySpan<byte>The secret key.
Exceptions
- ArgumentNullException
innerHash,outerHash, orkeyis null.
Properties
AlgorithmName
Gets the name of the MAC algorithm.
public string AlgorithmName { get; }
Property Value
MacSize
Gets the MAC output size in bytes.
public int MacSize { get; }
Property Value
Methods
ComputeHash(byte[])
Computes the HMAC tag for the given data in a single operation.
public byte[] ComputeHash(byte[] data)
Parameters
databyte[]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
dataReadOnlySpan<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
disposingboolTrue if called from Dispose.
Finalize(Span<byte>)
Computes the final MAC tag and writes it to destination.
public void Finalize(Span<byte> destination)
Parameters
Exceptions
- ArgumentException
destinationis 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
inputReadOnlySpan<byte>The data to process.
Exceptions
- ObjectDisposedException
Thrown when the instance has been disposed.