public class Mac extends java.lang.Object implements java.lang.Cloneable
The best way to describe a MAC is as a keyed one-way hash function, which looks like:
D = MAC(K, M)
where K is the key, M is the message,
and D is the resulting digest. One party will usually
send the concatenation M || D to the other party, who
will then verify D by computing D' in a
similar fashion. If D == D', then the message is assumed
to be authentic.
| Modifier | Constructor and Description |
|---|---|
protected |
Mac(MacSpi macSpi,
java.security.Provider provider,
java.lang.String algorithm)
Creates a new Mac instance.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.Object |
clone()
Clone this instance, if the underlying implementation supports it.
|
byte[] |
doFinal()
Finishes the computation of a MAC and returns the digest.
|
byte[] |
doFinal(byte[] input)
Finishes the computation of a MAC with a final byte array (or
computes a MAC over those bytes only) and returns the digest.
|
void |
doFinal(byte[] output,
int outOffset)
Finishes the computation of a MAC and places the result into the
given array.
|
java.lang.String |
getAlgorithm()
Returns the name of this MAC algorithm.
|
static Mac |
getInstance(java.lang.String algorithm)
Create an instance of the named algorithm from the first provider with an
appropriate implementation.
|
static Mac |
getInstance(java.lang.String algorithm,
java.security.Provider provider)
Create an instance of the named algorithm from a provider.
|
static Mac |
getInstance(java.lang.String algorithm,
java.lang.String provider)
Create an instance of the named algorithm from the named provider.
|
int |
getMacLength()
Get the size of the MAC.
|
java.security.Provider |
getProvider()
Get the provider of the underlying implementation.
|
void |
init(java.security.Key key)
Initialize this MAC with a key and no parameters.
|
void |
init(java.security.Key key,
java.security.spec.AlgorithmParameterSpec params)
Initialize this MAC with a key and parameters.
|
void |
reset()
Reset this instance.
|
void |
update(byte input)
Update the computation with a single byte.
|
void |
update(byte[] input)
Update the computation with a byte array.
|
void |
update(byte[] input,
int offset,
int length)
Update the computation with a portion of a byte array.
|
void |
update(java.nio.ByteBuffer buffer)
Update this MAC with the remaining bytes in the given buffer
|
public static final Mac getInstance(java.lang.String algorithm) throws java.security.NoSuchAlgorithmException
algorithm - The name of the algorithm.java.security.NoSuchAlgorithmException - If no implementation of the named
algorithm is installed.java.lang.IllegalArgumentException - if algorithm is
null or is an empty string.public static final Mac getInstance(java.lang.String algorithm, java.lang.String provider) throws java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException
algorithm - The name of the algorithm.provider - The name of the provider.java.security.NoSuchAlgorithmException - If the named provider has no
implementation of the algorithm.java.security.NoSuchProviderException - If the named provider does not exist.java.lang.IllegalArgumentException - if either algorithm or
provider is null, or if
algorithm is an empty string.public static final Mac getInstance(java.lang.String algorithm, java.security.Provider provider) throws java.security.NoSuchAlgorithmException
algorithm - The name of the algorithm.provider - The provider.java.security.NoSuchAlgorithmException - If the provider has no implementation of
the algorithm.java.lang.IllegalArgumentException - if either algorithm or
provider is null, or if
algorithm is an empty string.public final byte[] doFinal() throws java.lang.IllegalStateException
After this method succeeds, it may be used again as just after a
call to init, and can compute another MAC using the
same key and parameters.
java.lang.IllegalStateException - If this instnace has not
been initialized.public final byte[] doFinal(byte[] input) throws java.lang.IllegalStateException
After this method succeeds, it may be used again as just after a
call to init, and can compute another MAC using the
same key and parameters.
input - The bytes to add.java.lang.IllegalStateException - If this instnace has not
been initialized.public final void doFinal(byte[] output, int outOffset) throws java.lang.IllegalStateException, ShortBufferException
After this method succeeds, it may be used again as just after a
call to init, and can compute another MAC using the
same key and parameters.
output - The destination for the result.outOffset - The index in the output array to start.java.lang.IllegalStateException - If this instnace has not
been initialized.ShortBufferException - If output is
not large enough to hold the result.public final java.lang.String getAlgorithm()
public final int getMacLength()
doFinal() and doFinal(byte[]), and the minimum
number of bytes that must be available in the byte array passed to
doFinal(byte[],int).public final java.security.Provider getProvider()
public final void init(java.security.Key key) throws java.security.InvalidKeyException
key - The key to initialize this instance with.java.security.InvalidKeyException - If the key is
unacceptable.public final void init(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException
key - The key to initialize this instance with.params - The algorithm-specific parameters.java.security.InvalidAlgorithmParameterException - If the
algorithm parameters are unacceptable.java.security.InvalidKeyException - If the key is
unacceptable.public final void reset()
public final void update(byte input) throws java.lang.IllegalStateException
input - The next byte.java.lang.IllegalStateException - If this instance has not
been initialized.public final void update(byte[] input) throws java.lang.IllegalStateException
input - The next bytes.java.lang.IllegalStateException - If this instance has not
been initialized.public final void update(byte[] input, int offset, int length) throws java.lang.IllegalStateException
input - The next bytes.offset - The index in input to start.length - The number of bytes to update.java.lang.IllegalStateException - If this instance has not
been initialized.public final void update(java.nio.ByteBuffer buffer)
buffer - The input buffer.public final java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone in class java.lang.Objectjava.lang.CloneNotSupportedException - If the underlying
implementation is not cloneable.Cloneable