Class HmacSpec

java.lang.Object
zeroecho.core.alg.hmac.HmacSpec
All Implemented Interfaces:
Describable, ContextSpec

public final class HmacSpec extends Object implements ContextSpec, Describable

Specification for HMAC operation parameters

HmacSpec identifies the HMAC variant (digest family) to be used in a MacContext. It does not include key material; keys are provided separately via SecretKey.

Fields

  • macName: the canonical algorithm name as understood by the JCA (e.g., "HmacSHA256", "HmacSHA384", "HmacSHA512").

Usage

This spec is passed when creating a new HMAC context:
SecretKey key = session.keyBuilders().symmetric().generate("HMAC",
    HmacKeyGenSpec.sha256(256));

MacContext ctx = session.createContext("HMAC",
    KeyUsage.MAC, key, HmacSpec.sha256());

ctx.update(data);
byte[] tag = ctx.doFinal();

Defaults

Convenience factories are provided for the most common variants:
Since:
1.0
  • Constructor Details

    • HmacSpec

      public HmacSpec(String macName)
      Constructs a new HMAC spec for the given algorithm.
      Parameters:
      macName - canonical algorithm name (e.g., "HmacSHA256")
      Throws:
      NullPointerException - if macName is null
  • Method Details

    • macName

      public String macName()
      Returns the canonical JCA algorithm name of this spec.
      Returns:
      algorithm name (e.g., "HmacSHA256")
    • sha256

      public static HmacSpec sha256()
      Convenience factory for HMAC-SHA256.
      Returns:
      new spec targeting HmacSHA256
    • sha384

      public static HmacSpec sha384()
      Convenience factory for HMAC-SHA384.
      Returns:
      new spec targeting HmacSHA384
    • sha512

      public static HmacSpec sha512()
      Convenience factory for HMAC-SHA512.
      Returns:
      new spec targeting HmacSHA512
    • description

      public String description()
      Returns a human-readable description of this spec.

      The description is simply the algorithm name (e.g., "HmacSHA256").

      Specified by:
      description in interface Describable
      Returns:
      algorithm name string
    • recommendedKeyBits

      public int recommendedKeyBits()
      Returns a recommended key size (in bits) for this HMAC variant.

      HMAC is defined for keys of arbitrary length; this method therefore does not express a strict requirement. It provides a conservative, interoperability-friendly recommendation intended for default key derivation and key generation paths, especially where the caller does not want to manually select a key size.

      The recommendation follows common practice: use a key size at least equal to the underlying hash output length. For the built-in variants this yields:

      • HmacSHA256 - 256 bits
      • HmacSHA384 - 384 bits
      • HmacSHA512 - 512 bits

      If this spec uses an unrecognized macName() value, the method returns 256 bits as a safe default and to avoid failing existing applications that rely on custom provider names. Applications with strict requirements should enforce their own policy and/or explicitly specify a key size.

      Returns:
      recommended key size in bits (positive, multiple of 8)
      Since:
      1.0