Class HmacSpec
- All Implemented Interfaces:
Describable, ContextSpec
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a human-readable description of this spec.macName()Returns the canonical JCA algorithm name of this spec.intReturns a recommended key size (in bits) for this HMAC variant.static HmacSpecsha256()Convenience factory for HMAC-SHA256.static HmacSpecsha384()Convenience factory for HMAC-SHA384.static HmacSpecsha512()Convenience factory for HMAC-SHA512.
-
Constructor Details
-
HmacSpec
Constructs a new HMAC spec for the given algorithm.- Parameters:
macName- canonical algorithm name (e.g.,"HmacSHA256")- Throws:
NullPointerException- ifmacNameisnull
-
-
Method Details
-
macName
Returns the canonical JCA algorithm name of this spec.- Returns:
- algorithm name (e.g.,
"HmacSHA256")
-
sha256
Convenience factory for HMAC-SHA256.- Returns:
- new spec targeting
HmacSHA256
-
sha384
Convenience factory for HMAC-SHA384.- Returns:
- new spec targeting
HmacSHA384
-
sha512
Convenience factory for HMAC-SHA512.- Returns:
- new spec targeting
HmacSHA512
-
description
Returns a human-readable description of this spec.The description is simply the algorithm name (e.g.,
"HmacSHA256").- Specified by:
descriptionin interfaceDescribable- 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 returns256bits 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
-