public class HMac
extends javax.crypto.MacSpi
javax.crypto.MACSpi
class for providing
the functionality of the HMAC(Keyed-Hashing for Message Authentication)
algorithm, as specified in RFC 2104.
For HMAC computation based the secret authentication key to be used may be of any length up to the hash algorithm block size. If the key is longer than the block size, it is hashed by the hash function in use (before feeding it to the HMAC algorithm), yielding to a key length denoted by the hash size of the actual hash function (i.e. 16 bytes for Md5 and 20 bytes for SHA). It is recommended by RFC 2104 not to use keys of a length shorter than the hash output size of the hash function in use.
Any application dealing with MAC computation, uses the getInstance
method of the Mac
class for creating a MAC object.
A Message Authentication Code (MAC) denotes a cryptographic checksum, which is derived by processing some given message (or the authentication elements of the message) using a secret key. A MAC computation that involves some cryptographic hash function is denoted as HMAC (as specified in RFC 2104). Since a secret key is used for processing the message, the resulting (H)MAC only can be verified with the same key, meaning that - in contrast to digital signing where anyone can verify a signature by using the public key matching to the private key that have been used for signing - only the holder of the same secret key is able to verify the MAC. Commonly, only the addressed receiver(s) of the message should hold the same secret key.
MAC computation can be used for providing integrity without secrecy. The sender uses his secret key for computing the MAC of the message to be sent, appends the MAC to the original message and sends both to the communication partner. The receiver recalculates the MAC and compares it with the transmitted MAC to ensure the integrity of the received data.
An application wishing to perform a MAC computation, at first has to create
a MAC object by instantiating the Mac
class through a proper
getInstance
factory method thereby specifying the MAC algorithm
to be used. Calculating, for instance, a HMAC based on the SHA-1 hash
algorithm, may be initiated as follows:
Mac sha_HMAC = Mac.getInstance("HMAC/SHA");
After properly initializing the MAC object with one entity's secret key, the
data to be processed is applied by one (or more) calls to the update
methods. The MAC computation is concluded by using doFinal
.
If the data can be processed without calling any update
method,
doFinal
can be called immediately after initializing the MAC
object:
sha_HMAC.init(secret_key); byte[] mac_data = sha_HMAC.doFinal(data);
After the MAC finally has been calculated, the MAC object automatically is reset for being able to be used for further MAC computations, either by using the same secret key again, or using a new key by properly re-initializing this MAC object.
HMacSha
,
HMacSha224
,
HMacSha256
,
HMacSha384
,
HMacSha512
,
HMacMd5
,
HMacRipeMd128
,
HMacRipeMd160
Constructor and Description |
---|
HMac(java.lang.String hashAlgorithm)
Creates a new HMac for the specified hash algorithm.
|
HMac(java.lang.String hashAlgorithm,
int blockSize)
Creates a new HMac for the specified hash algorithm.
|
Modifier and Type | Method and Description |
---|---|
byte[] |
engineDoFinal()
Returns the calculated MAC value.
|
int |
engineGetMacLength()
Returns the length of the calculated MAC value in bytes.
|
void |
engineInit(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Initializes this Mac object with given secret key and algorithm parameter
specification.
|
void |
engineReset()
Resets this Mac object for being able to be used for further MAC
computations, either by using the same secret key again, or using
a new key by properly re-initializing this MAC object.
|
void |
engineUpdate(byte input)
Processes the given byte.
|
void |
engineUpdate(byte[] input,
int offset,
int len)
Processes the given number of bytes, supplied in a byte array
starting at the given position.
|
public HMac(java.lang.String hashAlgorithm) throws java.security.NoSuchAlgorithmException
This constructor is called by every subclass for specifying the particular hash algorithm to be used for HMAC computation. The block size of the underlying hash algorithm defaults to 64;
hashAlgorithm
- the hash algorithm to usejava.security.NoSuchAlgorithmException
- if the specified hash algorithm is not supportedpublic HMac(java.lang.String hashAlgorithm, int blockSize) throws java.security.NoSuchAlgorithmException
This constructor maybe called by any subclass for specifying the particular hash algorithm to be used for HMAC computation.
hashAlgorithm
- the hash algorithm to useblockSize
- the block size of the hash algorithmjava.security.NoSuchAlgorithmException
- if the specified hash algorithm is not supportedpublic void engineInit(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidKeyException
The length of the authentication key to be used may be of any length up to hash algorithm block size. If the key is longer than the block size, it is automatically hashed by the actual hash function in use before feeding it to the HMAC algorithm, yielding to a key length denoted by the hash size of the actual hash function (i.e. 16 bytes for Md5 and 20 bytes for SHA). It is recommended by RFC 2104 not to use keys of a length shorter than the hash output size of the hash function in use.
engineInit
in class javax.crypto.MacSpi
key
- the secret key for initializing this MAC object.params
- the algorithm parameter specification; ignored because not required.java.security.InvalidKeyException
- if the given key cannot be used for initializing this MAC objectpublic void engineUpdate(byte input)
engineUpdate
in class javax.crypto.MacSpi
input
- the byte to be processed.public void engineUpdate(byte[] input, int offset, int len)
engineUpdate
in class javax.crypto.MacSpi
input
- the byte array holding the data to be processedoffset
- the offset indicating the start position within the input
byte arraylen
- the number of bytes to be processedpublic void engineReset()
engineReset
in class javax.crypto.MacSpi
public int engineGetMacLength()
engineGetMacLength
in class javax.crypto.MacSpi
public byte[] engineDoFinal()
engineDoFinal
in class javax.crypto.MacSpi