Class HmacKeyImportSpec

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

public final class HmacKeyImportSpec extends Object implements AlgorithmKeySpec, Describable, Destroyable
Specification for importing raw HMAC keys into a SecretKey.

HmacKeyImportSpec describes how existing key material for an HMAC algorithm (for example, "HmacSHA256") should be wrapped into a SecretKey. Unlike HmacKeyGenSpec, which requests generation of fresh random keys, this class is used when the caller already possesses key material in raw, hexadecimal, or Base64-encoded form.

Fields

  • macName: canonical HMAC algorithm name (for example, "HmacSHA256").
  • key: the raw key material as a byte array (defensively copied).

Typical usage

Import from raw bytes


byte[] rawKey = Files.readAllBytes(Paths.get("hmac.key"));
HmacKeyImportSpec spec = HmacKeyImportSpec.fromRaw("HmacSHA256", rawKey);
SecretKey key = session.keyBuilders().symmetric().importKey("HMAC", spec);

Import from hex/Base64


HmacKeyImportSpec specHex =
    HmacKeyImportSpec.fromHex("HmacSHA256", "aabbccddeeff00112233");

HmacKeyImportSpec specB64 =
    HmacKeyImportSpec.fromBase64("HmacSHA256", "Khsz95oU+7H0Ow==");

Serialization

Security notes

  • Imported keys are cloned defensively; callers should erase the original buffers if sensitive.
  • Validate the key length for the chosen digest family (for example, 256 bits for HmacSHA256).
  • Do not reuse the same key across different algorithms.
Since:
1.0
  • Constructor Details

    • HmacKeyImportSpec

      public HmacKeyImportSpec(String macName, byte[] key)
      Constructs a new HMAC key import specification.
      Parameters:
      macName - algorithm name (e.g., "HmacSHA256")
      key - raw key material; must not be null
      Throws:
      NullPointerException - if macName or key is null
  • Method Details

    • macName

      public String macName()
      Returns the HMAC algorithm name.
      Returns:
      algorithm name string (e.g., "HmacSHA256")
    • key

      public byte[] key()
      Returns a defensive copy of the raw key material.
      Returns:
      cloned key bytes
    • fromRaw

      public static HmacKeyImportSpec fromRaw(String macName, byte[] key)
      Creates a spec from raw bytes.
      Parameters:
      macName - algorithm name (e.g., "HmacSHA256")
      key - raw key material
      Returns:
      a new HmacKeyImportSpec
    • fromHex

      public static HmacKeyImportSpec fromHex(String macName, String hex)
      Creates a spec from a hex-encoded key string.
      Parameters:
      macName - algorithm name (e.g., "HmacSHA256")
      hex - key material encoded as hexadecimal
      Returns:
      a new HmacKeyImportSpec
      Throws:
      NullPointerException - if hex is null
    • fromBase64

      public static HmacKeyImportSpec fromBase64(String macName, String b64)
      Creates a spec from a Base64-encoded key string.
      Parameters:
      macName - algorithm name (e.g., "HmacSHA256")
      b64 - key material encoded in Base64
      Returns:
      a new HmacKeyImportSpec
      Throws:
      NullPointerException - if b64 is null
    • description

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

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

      Specified by:
      description in interface Describable
      Returns:
      algorithm name
    • marshal

      public static PairSeq marshal(HmacKeyImportSpec spec)
      Serializes a spec into a PairSeq.
      Parameters:
      spec - the spec to serialize
      Returns:
      encoded key spec sequence
    • unmarshal

      public static HmacKeyImportSpec unmarshal(PairSeq p)
      Deserializes a spec from a PairSeq.

      Accepts key material encoded as k.b64 (Base64) or k.hex (hexadecimal).

      Parameters:
      p - sequence containing encoded spec fields
      Returns:
      a reconstructed HmacKeyImportSpec
      Throws:
      IllegalArgumentException - if mac or key material is missing
    • destroy

      public void destroy()
      Specified by:
      destroy in interface Destroyable
    • isDestroyed

      public boolean isDestroyed()
      Specified by:
      isDestroyed in interface Destroyable