- All Implemented Interfaces:
Serializable, Key, SecretKey, Destroyable
- Enclosing class:
HMAC256Generator
This class provides an efficient SecretKey implementation that avoids unnecessary key data copying during construction for improved performance. Unlike standard SecretKeySpec, this implementation maintains a direct reference to the key data while maintaining compatibility with Mac operations.
Implementation Note: HmacSHA256 only uses the first 32 bytes of the key, so getEncoded() returns exactly those 32 bytes (zero padded if the caller supplied a shorter key); the full key data may be longer than 32 bytes. The copy is mandatory rather than an optimization: the JDK's HmacCore.engineInit() zeroes the array returned by getEncoded() once it has consumed it, so handing out the internal array would destroy the caller's key. This also means the result must never be cached - a second init() with the same key object would read back an all-zero key.
Field Details
_data
private final byte[] _dataThe data being authenticated.
Constructor Details
HMACKey
public HMACKey(byte[] data) Key data.- Parameters:
data- the key data
Method Details
getAlgorithm
- Specified by:
getAlgorithmin interfaceKey
getEncoded
public byte[] getEncoded()- Specified by:
getEncodedin interfaceKey
getFormat