Class HmacKeyGenSpec
java.lang.Object
zeroecho.core.alg.hmac.HmacKeyGenSpec
- All Implemented Interfaces:
Describable, AlgorithmKeySpec
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
macNamemust 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 registeredSymmetricKeyGenerator:
// 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 lengthsha384(int)- HMAC-SHA384 with a caller-specified bit lengthsha512(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 Summary
ConstructorsConstructorDescriptionHmacKeyGenSpec(String macName, int keySizeBits) Constructs a new specification for HMAC key generation. -
Method Summary
Modifier and TypeMethodDescriptionReturns a short description of this key spec.intReturns the key size requested for generation.macName()Returns the JCA algorithm name of the HMAC variant.static HmacKeyGenSpecsha256(int bits) Convenience factory for HMAC-SHA256 key generation specs.static HmacKeyGenSpecsha384(int bits) Convenience factory for HMAC-SHA384 key generation specs.static HmacKeyGenSpecsha512(int bits) Convenience factory for HMAC-SHA512 key generation specs.
-
Constructor Details
-
HmacKeyGenSpec
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- ifmacNameisnullIllegalArgumentException- ifkeySizeBitsis non-positive or not divisible by 8
-
-
Method Details
-
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
Convenience factory for HMAC-SHA256 key generation specs.- Parameters:
bits- desired key size in bits- Returns:
- a new spec targeting
HmacSHA256with the given key size
-
sha384
Convenience factory for HMAC-SHA384 key generation specs.- Parameters:
bits- desired key size in bits- Returns:
- a new spec targeting
HmacSHA384with the given key size
-
sha512
Convenience factory for HMAC-SHA512 key generation specs.- Parameters:
bits- desired key size in bits- Returns:
- a new spec targeting
HmacSHA512with the given key size
-
description
Returns a short description of this key spec.The description is formed as
"<macName>/<keySizeBits>". Example:"HmacSHA256/256".- Specified by:
descriptionin interfaceDescribable- Returns:
- human-readable description string
-