Class HmacKeyGenSpec

java.lang.Object
zeroecho.core.alg.hmac.HmacKeyGenSpec
All Implemented Interfaces:
Describable, AlgorithmKeySpec

public final class HmacKeyGenSpec extends Object implements AlgorithmKeySpec, Describable

Specification for HMAC key generation

HmacKeyGenSpec describes the parameters required to generate a secret key for a Hash-based Message Authentication Code (HMAC) algorithm. It defines the target MAC algorithm name and the key size in bits.

Constraints

  • The macName must correspond to a supported HMAC variant such as "HmacSHA256", "HmacSHA384", or "HmacSHA512".
  • The key size must be a positive multiple of 8 (i.e., full-byte lengths).

Usage

Typical usage is to construct a spec with a given digest family and key size, and then pass it to a registered SymmetricKeyGenerator:
// Generate a 256-bit key for HMAC-SHA256
HmacKeyGenSpec spec = HmacKeyGenSpec.sha256(256);
SecretKey key = session.keyBuilders().symmetric().generate("HMAC", spec);

Defaults

Convenience static factories are provided for the most common variants:
  • sha256(int) - HMAC-SHA256 with a caller-specified bit length
  • sha384(int) - HMAC-SHA384 with a caller-specified bit length
  • sha512(int) - HMAC-SHA512 with a caller-specified bit length

Security considerations

  • Recommended key size is at least equal to the output size of the underlying hash function (e.g., 256 bits for SHA-256).
  • Keys should always be generated with a strong randomness source.
  • Do not reuse HMAC keys across different digest families.
Since:
1.0
  • Constructor Details

    • HmacKeyGenSpec

      public HmacKeyGenSpec(String macName, int keySizeBits)
      Constructs a new specification for HMAC key generation.
      Parameters:
      macName - canonical name of the target HMAC algorithm (e.g., "HmacSHA256")
      keySizeBits - desired key size in bits; must be a positive multiple of 8
      Throws:
      NullPointerException - if macName is null
      IllegalArgumentException - if keySizeBits is non-positive or not divisible by 8
  • Method Details

    • macName

      public String macName()
      Returns the JCA algorithm name of the HMAC variant.
      Returns:
      the algorithm name (e.g., "HmacSHA256")
    • keySizeBits

      public int keySizeBits()
      Returns the key size requested for generation.
      Returns:
      key size in bits (always a positive multiple of 8)
    • sha256

      public static HmacKeyGenSpec sha256(int bits)
      Convenience factory for HMAC-SHA256 key generation specs.
      Parameters:
      bits - desired key size in bits
      Returns:
      a new spec targeting HmacSHA256 with the given key size
    • sha384

      public static HmacKeyGenSpec sha384(int bits)
      Convenience factory for HMAC-SHA384 key generation specs.
      Parameters:
      bits - desired key size in bits
      Returns:
      a new spec targeting HmacSHA384 with the given key size
    • sha512

      public static HmacKeyGenSpec sha512(int bits)
      Convenience factory for HMAC-SHA512 key generation specs.
      Parameters:
      bits - desired key size in bits
      Returns:
      a new spec targeting HmacSHA512 with the given key size
    • description

      public String description()
      Returns a short description of this key spec.

      The description is formed as "<macName>/<keySizeBits>". Example: "HmacSHA256/256".

      Specified by:
      description in interface Describable
      Returns:
      human-readable description string