Package javax.crypto
Class Mac
java.lang.Object
javax.crypto.Mac
- All Implemented Interfaces:
Cloneable
public class Mac extends Object implements Cloneable
This class provides the public API for Message Authentication Code
(MAC) algorithms.
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description Objectclone()Clones thisMacinstance and the underlying implementation.byte[]doFinal()Computes the digest of this MAC based on the data previously specified inupdate(byte)calls.byte[]doFinal(byte[] input)Computes the digest of this MAC based on the data previously specified onupdate(byte)calls and on the final bytes specified byinput(or based on those bytes only).voiddoFinal(byte[] output, int outOffset)Computes the digest of this MAC based on the data previously specified inupdate(byte)calls and stores the digest in the specifiedoutputbuffer at offsetoutOffset.StringgetAlgorithm()Returns the name of the MAC algorithm.static MacgetInstance(String algorithm)Creates a newMacinstance that provides the specified MAC algorithm.static MacgetInstance(String algorithm, String provider)Creates a newMacinstance that provides the specified MAC algorithm from the specified provider.static MacgetInstance(String algorithm, Provider provider)Creates a newMacinstance that provides the specified MAC algorithm from the specified provider.intgetMacLength()Returns the length of this MAC (in bytes).ProvidergetProvider()Returns the provider of thisMacinstance.voidinit(Key key)Initializes thisMacinstance with the specified key.voidinit(Key key, AlgorithmParameterSpec params)Initializes thisMacinstance with the specified key and algorithm parameters.voidreset()Resets thisMacinstance to its initial state.voidupdate(byte input)Updates thisMacinstance with the specified byte.voidupdate(byte[] input)Copies the buffer provided as input for further processing.voidupdate(byte[] input, int offset, int len)Updates thisMacinstance with the data from the specified bufferinputfrom the specifiedoffsetand lengthlen.voidupdate(ByteBuffer input)Updates thisMacinstance with the data from the specified buffer, starting atBuffer.position(), including the nextBuffer.remaining()bytes.
-
Constructor Details
-
Mac
Creates a newMacinstance.- Parameters:
macSpi- the implementation delegate.provider- the implementation provider.algorithm- the name of the MAC algorithm.
-
-
Method Details
-
getAlgorithm
Returns the name of the MAC algorithm.- Returns:
- the name of the MAC algorithm.
-
getProvider
Returns the provider of thisMacinstance.- Returns:
- the provider of this
Macinstance.
-
getInstance
Creates a newMacinstance that provides the specified MAC algorithm.- Parameters:
algorithm- the name of the requested MAC algorithm.- Returns:
- the new
Macinstance. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available by any provider.NullPointerException- ifalgorithmisnull(instead of NoSuchAlgorithmException as in 1.4 release).
-
getInstance
public static final Mac getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderExceptionCreates a newMacinstance that provides the specified MAC algorithm from the specified provider.- Parameters:
algorithm- the name of the requested MAC algorithm.provider- the name of the provider that is providing the algorithm.- Returns:
- the new
Macinstance. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not provided by the specified provider.NoSuchProviderException- if the specified provider is not available.IllegalArgumentException- if the specified provider name isnullor empty.NullPointerException- ifalgorithmisnull(instead of NoSuchAlgorithmException as in 1.4 release).
-
getInstance
public static final Mac getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmExceptionCreates a newMacinstance that provides the specified MAC algorithm from the specified provider.- Parameters:
algorithm- the name of the requested MAC algorithm.provider- the provider that is providing the algorithm.- Returns:
- the new
Macinstance. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not provided by the specified provider.IllegalArgumentException- ifproviderisnull.NullPointerException- ifalgorithmisnull(instead of NoSuchAlgorithmException as in 1.4 release).
-
getMacLength
public final int getMacLength()Returns the length of this MAC (in bytes).- Returns:
- the length of this MAC (in bytes).
-
init
public final void init(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterExceptionInitializes thisMacinstance with the specified key and algorithm parameters.- Parameters:
key- the key to initialize this algorithm.params- the parameters for this algorithm.- Throws:
InvalidKeyException- if the specified key cannot be used to initialize this algorithm, or it is null.InvalidAlgorithmParameterException- if the specified parameters cannot be used to initialize this algorithm.
-
init
Initializes thisMacinstance with the specified key.- Parameters:
key- the key to initialize this algorithm.- Throws:
InvalidKeyException- if initialization fails because the provided key isnull.RuntimeException- if the specified key cannot be used to initialize this algorithm.
-
update
Updates thisMacinstance with the specified byte.- Parameters:
input- the byte- Throws:
IllegalStateException- if this MAC is not initialized.
-
update
Updates thisMacinstance with the data from the specified bufferinputfrom the specifiedoffsetand lengthlen.- Parameters:
input- the buffer.offset- the offset in the buffer.len- the length of the data in the buffer.- Throws:
IllegalStateException- if this MAC is not initialized.IllegalArgumentException- ifoffsetandlendo not specified a valid chunk ininputbuffer.
-
update
Copies the buffer provided as input for further processing.- Parameters:
input- the buffer.- Throws:
IllegalStateException- if this MAC is not initialized.
-
update
Updates thisMacinstance with the data from the specified buffer, starting atBuffer.position(), including the nextBuffer.remaining()bytes.- Parameters:
input- the buffer.- Throws:
IllegalStateException- if this MAC is not initialized.
-
doFinal
Computes the digest of this MAC based on the data previously specified inupdate(byte)calls.This
Macinstance is reverted to its initial state and can be used to start the next MAC computation with the same parameters or initialized with different parameters.- Returns:
- the generated digest.
- Throws:
IllegalStateException- if this MAC is not initialized.
-
doFinal
public final void doFinal(byte[] output, int outOffset) throws ShortBufferException, IllegalStateExceptionComputes the digest of this MAC based on the data previously specified inupdate(byte)calls and stores the digest in the specifiedoutputbuffer at offsetoutOffset.This
Macinstance is reverted to its initial state and can be used to start the next MAC computation with the same parameters or initialized with different parameters.- Parameters:
output- the output bufferoutOffset- the offset in the output buffer- Throws:
ShortBufferException- if the specified output buffer is either too small for the digest to be stored, the specified output buffer isnull, or the specified offset is negative or past the length of the output buffer.IllegalStateException- if this MAC is not initialized.
-
doFinal
Computes the digest of this MAC based on the data previously specified onupdate(byte)calls and on the final bytes specified byinput(or based on those bytes only).This
Macinstance is reverted to its initial state and can be used to start the next MAC computation with the same parameters or initialized with different parameters.- Parameters:
input- the final bytes.- Returns:
- the generated digest.
- Throws:
IllegalStateException- if this MAC is not initialized.
-
reset
public final void reset()Resets thisMacinstance to its initial state.This
Macinstance is reverted to its initial state and can be used to start the next MAC computation with the same parameters or initialized with different parameters. -
clone
Clones thisMacinstance and the underlying implementation.- Overrides:
clonein classObject- Returns:
- the cloned instance.
- Throws:
CloneNotSupportedException- if the underlying implementation does not support cloning.
-