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
marshal(HmacKeyImportSpec)encodes a spec into aPairSeqwith fieldstype=HMAC-KEY,mac, andk.b64(Base64 without padding).unmarshal(PairSeq)decodes a spec, acceptingk.b64ork.hex.
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 Summary
ConstructorsConstructorDescriptionHmacKeyImportSpec(String macName, byte[] key) Constructs a new HMAC key import specification. -
Method Summary
Modifier and TypeMethodDescriptionReturns a short human-readable description of this spec.voiddestroy()static HmacKeyImportSpecfromBase64(String macName, String b64) Creates a spec from a Base64-encoded key string.static HmacKeyImportSpecCreates a spec from a hex-encoded key string.static HmacKeyImportSpecCreates a spec from raw bytes.booleanbyte[]key()Returns a defensive copy of the raw key material.macName()Returns the HMAC algorithm name.static PairSeqmarshal(HmacKeyImportSpec spec) Serializes a spec into aPairSeq.static HmacKeyImportSpecDeserializes a spec from aPairSeq.
-
Constructor Details
-
HmacKeyImportSpec
Constructs a new HMAC key import specification.- Parameters:
macName- algorithm name (e.g.,"HmacSHA256")key- raw key material; must not benull- Throws:
NullPointerException- ifmacNameorkeyisnull
-
-
Method Details
-
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
Creates a spec from raw bytes.- Parameters:
macName- algorithm name (e.g.,"HmacSHA256")key- raw key material- Returns:
- a new
HmacKeyImportSpec
-
fromHex
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- ifhexisnull
-
fromBase64
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- ifb64isnull
-
description
Returns a short human-readable description of this spec.The description is the algorithm name (e.g.,
"HmacSHA256").- Specified by:
descriptionin interfaceDescribable- Returns:
- algorithm name
-
marshal
Serializes a spec into aPairSeq.- Parameters:
spec- the spec to serialize- Returns:
- encoded key spec sequence
-
unmarshal
Deserializes a spec from aPairSeq.Accepts key material encoded as
k.b64(Base64) ork.hex(hexadecimal).- Parameters:
p- sequence containing encoded spec fields- Returns:
- a reconstructed
HmacKeyImportSpec - Throws:
IllegalArgumentException- ifmacor key material is missing
-
destroy
public void destroy()- Specified by:
destroyin interfaceDestroyable
-
isDestroyed
public boolean isDestroyed()- Specified by:
isDestroyedin interfaceDestroyable
-