using System; using System.IO; using System.Threading; using System.Threading.Tasks; namespace StellaOps.Cryptography; /// /// Interface for HMAC (Hash-based Message Authentication Code) operations with compliance profile support. /// public interface ICryptoHmac { #region Purpose-based methods (preferred for compliance) /// /// Computes an HMAC for the specified purpose using the active compliance profile's algorithm. /// /// The secret key. /// The data to authenticate. /// The HMAC purpose from . /// The HMAC bytes. byte[] ComputeHmacForPurpose(ReadOnlySpan key, ReadOnlySpan data, string purpose); /// /// Computes an HMAC for the specified purpose and returns it as a lowercase hex string. /// /// The secret key. /// The data to authenticate. /// The HMAC purpose from . /// The HMAC as a lowercase hex string. string ComputeHmacHexForPurpose(ReadOnlySpan key, ReadOnlySpan data, string purpose); /// /// Computes an HMAC for the specified purpose and returns it as a Base64 string. /// /// The secret key. /// The data to authenticate. /// The HMAC purpose from . /// The HMAC as a Base64 string. string ComputeHmacBase64ForPurpose(ReadOnlySpan key, ReadOnlySpan data, string purpose); /// /// Computes an HMAC for the specified purpose from a stream asynchronously. /// /// The secret key. /// The stream to authenticate. /// The HMAC purpose from . /// Cancellation token. /// The HMAC bytes. ValueTask ComputeHmacForPurposeAsync(ReadOnlyMemory key, Stream stream, string purpose, CancellationToken cancellationToken = default); /// /// Computes an HMAC for the specified purpose from a stream and returns it as a lowercase hex string. /// /// The secret key. /// The stream to authenticate. /// The HMAC purpose from . /// Cancellation token. /// The HMAC as a lowercase hex string. ValueTask ComputeHmacHexForPurposeAsync(ReadOnlyMemory key, Stream stream, string purpose, CancellationToken cancellationToken = default); #endregion #region Verification methods (constant-time comparison) /// /// Verifies an HMAC for the specified purpose using constant-time comparison. /// /// The secret key. /// The data that was authenticated. /// The expected HMAC value. /// The HMAC purpose from . /// True if the HMAC matches; otherwise, false. bool VerifyHmacForPurpose(ReadOnlySpan key, ReadOnlySpan data, ReadOnlySpan expectedHmac, string purpose); /// /// Verifies an HMAC for the specified purpose using constant-time comparison (hex format). /// /// The secret key. /// The data that was authenticated. /// The expected HMAC value as a hex string. /// The HMAC purpose from . /// True if the HMAC matches; otherwise, false. bool VerifyHmacHexForPurpose(ReadOnlySpan key, ReadOnlySpan data, string expectedHmacHex, string purpose); /// /// Verifies an HMAC for the specified purpose using constant-time comparison (Base64 format). /// /// The secret key. /// The data that was authenticated. /// The expected HMAC value as a Base64 string. /// The HMAC purpose from . /// True if the HMAC matches; otherwise, false. bool VerifyHmacBase64ForPurpose(ReadOnlySpan key, ReadOnlySpan data, string expectedHmacBase64, string purpose); #endregion #region Metadata methods /// /// Gets the algorithm that will be used for the specified purpose based on the active compliance profile. /// /// The HMAC purpose from . /// The algorithm identifier (e.g., "HMAC-SHA256", "HMAC-GOST3411"). string GetAlgorithmForPurpose(string purpose); /// /// Gets the expected HMAC output length in bytes for the specified purpose. /// /// The HMAC purpose from . /// The output length in bytes. int GetOutputLengthForPurpose(string purpose); #endregion }