public abstract class HMac
extends javax.crypto.MacSpi
Algorithms that provide a way to check the integrity of information based on a secret key are called message authentication codes (MAC). Typically, message authentication codes are used between two parties that share a secret key in order to validate information transmitted between these parties. An HMAC is simply a MAC algorithm based on a cryptographic hash function.
HMAC can be used in combination with any iterated cryptographic message digest algorithm (SHA-1, SHA-256, ...). It requires a secret key for the calculation and verification of the message authentication values. This key can be of any length, however, if it is longer than the block size of the underlying message digest algorithm, it is automatically internally digested using the message digest algorithm and the result is then used as the key. As a result, long keys do not significantly increase the strength of the algorithm (using longer keys may be advisable if the randomness of the key is considered weak). It is recommended that the key be at least as large as the output size of the message digest algorithm; using shorter keys will decrease the security strength of the algorithm.
An HMAC algorithm instance can be obtained using the Java Cryptography
Architecture (JCA), by requesting the '<algorithm>' MAC from the
Entrust cryptographic
service provider. This can be done using the following call:
Mac.getInstance("<algorithm>", "Entrust");
The following key types are currently supported:
TokenSymmetricKey.| Modifier and Type | Method and Description |
|---|---|
protected byte[] |
engineDoFinal()
Completes the MAC computation and resets the MAC for further use,
maintaining the secret key that the MAC was initialized with.
|
protected int |
engineGetMacLength()
Returns the length of the MAC in bytes.
|
protected void |
engineInit(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Initializes the MAC with the given (secret) key and algorithm parameters.
|
protected void |
engineReset()
Resets the MAC for further use, maintaining the secret key that the MAC
was initialized with.
|
protected void |
engineUpdate(byte input)
Processes the given byte.
|
protected void |
engineUpdate(byte[] input,
int offset,
int len)
Processes the first
len bytes in input,
starting at offset inclusive. |
protected final void engineInit(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
throws java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
engineInit in class javax.crypto.MacSpikey - [FIPS 140-2 data input] [FIPS 140-2 CSP] the (secret) key.params - [FIPS 140-2 data input] the algorithm parameters.java.security.InvalidKeyException - [FIPS 140-2 status output] if the given key is
inappropriate for initializing this MAC.java.security.InvalidAlgorithmParameterException - [FIPS 140-2 status output] if the given algorithm
parameters are inappropriate for this MAC.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsprotected final byte[] engineDoFinal()
engineDoFinal in class javax.crypto.MacSpiFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsprotected final void engineUpdate(byte[] input,
int offset,
int len)
len bytes in input,
starting at offset inclusive.
engineUpdate in class javax.crypto.MacSpiinput - [FIPS 140-2 data input] the input buffer.offset - [FIPS 140-2 data input] the offset in input where
the input starts.len - [FIPS 140-2 data input] the number of bytes to process.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsprotected final void engineUpdate(byte input)
engineUpdate in class javax.crypto.MacSpiinput - [FIPS 140-2 data input] the input byte to be processed.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsprotected final void engineReset()
engineReset in class javax.crypto.MacSpiFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsprotected final int engineGetMacLength()
engineGetMacLength in class javax.crypto.MacSpiFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operations