APIdia Launch Partner
Top Description Fields Constructors Methods
javax.crypto

public Class Mac

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.util.*, java.security.*, .Provider.Service, java.security.spec.AlgorithmParameterSpec, java.nio.ByteBuffer, sun.security.util.Debug, sun.security.jca.*, .GetInstance.Instance

This class provides the functionality of a "Message Authentication Code" (MAC) algorithm.

A MAC provides a way to check the integrity of information transmitted over or stored in an unreliable medium, based on a secret key. Typically, message authentication codes are used between two parties that share a secret key in order to validate information transmitted between these parties.

A MAC mechanism that is based on cryptographic hash functions is referred to as HMAC. HMAC can be used with any cryptographic hash function, e.g., SHA256 or SHA384, in combination with a secret shared key. HMAC is specified in RFC 2104.

Every implementation of the Java platform is required to support the following standard Mac algorithms:

These algorithms are described in the Mac section of the Java Security Standard Algorithm Names Specification. Consult the release documentation for your implementation to see if any other algorithms are supported.
Author
Jan Luehe
Since
1.4
External Specification
Java Security Standard Algorithm Names

Field Summary

Modifier and TypeField and Description
private final String
private static final Debug
private Provider.Service
private boolean
private final Object
private static final Debug
private Provider
private Iterator<Provider.Service>
private static final boolean
private MacSpi
private static int

Constructor Summary

AccessConstructor and Description
protected
Mac(MacSpi
the delegate
macSpi
,
Provider
the provider
provider
,
String
the algorithm
algorithm
)

Creates a MAC object.

private

Method Summary

Modifier and TypeMethod and Description
pack-priv void
chooseFirstProvider()

Choose the Spi from the first provider available.

private void
public final Object

Returns:

a clone if the provider implementation is cloneable.
clone
()

Overrides java.lang.Object.clone.

Returns a clone if the provider implementation is cloneable.

public final byte[]

Returns:

the MAC result.
doFinal
()

Finishes the MAC operation.

public final void
doFinal(byte[]
the buffer where the MAC result is stored
output
,
int
the offset in output where the MAC is stored
outOffset
)

Finishes the MAC operation.

public final byte[]

Returns:

the MAC result.
doFinal
(byte[]
data in bytes
input
)

Processes the given array of bytes and finishes the MAC operation.

public final String

Returns:

the algorithm name of this Mac object.
getAlgorithm
()

Returns the algorithm name of this Mac object.

public static final Mac

Returns:

the new Mac object
getInstance
(String
the standard name of the requested MAC algorithm. See the Mac section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
)

Returns a Mac object that implements the specified MAC algorithm.

public static final Mac

Returns:

the new Mac object
getInstance
(String
the standard name of the requested MAC algorithm. See the Mac section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
,
String
the name of the provider.
provider
)

Returns a Mac object that implements the specified MAC algorithm.

public static final Mac

Returns:

the new Mac object
getInstance
(String
the standard name of the requested MAC algorithm. See the Mac section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
algorithm
,
Provider
the provider.
provider
)

Returns a Mac object that implements the specified MAC algorithm.

public final int

Returns:

the MAC length in bytes.
getMacLength
()

Returns the length of the MAC in bytes.

public final Provider

Returns:

the provider of this Mac object.
getProvider
()

Returns the provider of this Mac object.

private String
public final void
init(Key
the key.
key
)

Initializes this Mac object with the given key.

public final void
init(Key
the key.
key
,
AlgorithmParameterSpec
the algorithm parameters.
params
)

Initializes this Mac object with the given key and algorithm parameters.

public final void
reset()

Resets this Mac object.

public final void
update(byte
the input byte to be processed.
input
)

Processes the given byte.

public final void
update(byte[]
the array of bytes to be processed.
input
)

Processes the given array of bytes.

public final void
update(byte[]
the input buffer.
input
,
int
the offset in input where the input starts.
offset
,
int
the number of bytes to process.
len
)

Processes the first len bytes in input, starting at offset inclusive.

public final void
update(ByteBuffer
the ByteBuffer
input
)

Processes input.remaining() bytes in the ByteBuffer input, starting at input.position().

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

algorithmback to summary
private final String algorithm
debugback to summary
private static final Debug debug
firstServiceback to summary
private Provider.Service firstService
initializedback to summary
private boolean initialized
lockback to summary
private final Object lock
pdebugback to summary
private static final Debug pdebug
providerback to summary
private Provider provider
serviceIteratorback to summary
private Iterator<Provider.Service> serviceIterator
skipDebugback to summary
private static final boolean skipDebug
spiback to summary
private MacSpi spi
warnCountback to summary
private static int warnCount

Constructor Detail

Macback to summary
protected Mac(MacSpi macSpi, Provider provider, String algorithm)

Creates a MAC object.

Parameters
macSpi:MacSpi

the delegate

provider:Provider

the provider

algorithm:String

the algorithm

Macback to summary
private Mac(Provider.Service s, Iterator<Provider.Service> t, String algorithm)

Method Detail

chooseFirstProviderback to summary
pack-priv void chooseFirstProvider()

Choose the Spi from the first provider available. Used if delayed provider selection is not possible because init() is not the first method called.

chooseProviderback to summary
private void chooseProvider(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException
cloneback to summary
public final Object clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Returns a clone if the provider implementation is cloneable.

Returns:Object

a clone if the provider implementation is cloneable.

Exceptions
CloneNotSupportedException:
if this is called on a delegate that does not support Cloneable.
doFinalback to summary
public final byte[] doFinal() throws IllegalStateException

Finishes the MAC operation.

A call to this method resets this Mac object to the state it was in when previously initialized via a call to init(Key) or init(Key, AlgorithmParameterSpec). That is, the object is reset and available to generate another MAC from the same key, if desired, via new calls to update and doFinal. (In order to reuse this Mac object with a different key, it must be reinitialized via a call to init(Key) or init(Key, AlgorithmParameterSpec).

Returns:byte[]

the MAC result.

Exceptions
IllegalStateException:
if this Mac has not been initialized.
doFinalback to summary
public final void doFinal(byte[] output, int outOffset) throws ShortBufferException, IllegalStateException

Finishes the MAC operation.

A call to this method resets this Mac object to the state it was in when previously initialized via a call to init(Key) or init(Key, AlgorithmParameterSpec). That is, the object is reset and available to generate another MAC from the same key, if desired, via new calls to update and doFinal. (In order to reuse this Mac object with a different key, it must be reinitialized via a call to init(Key) or init(Key, AlgorithmParameterSpec).

The MAC result is stored in output, starting at outOffset inclusive.

Parameters
output:byte[]

the buffer where the MAC result is stored

outOffset:int

the offset in output where the MAC is stored

Exceptions
ShortBufferException:
if the given output buffer is too small to hold the result
IllegalStateException:
if this Mac has not been initialized.
doFinalback to summary
public final byte[] doFinal(byte[] input) throws IllegalStateException

Processes the given array of bytes and finishes the MAC operation.

A call to this method resets this Mac object to the state it was in when previously initialized via a call to init(Key) or init(Key, AlgorithmParameterSpec). That is, the object is reset and available to generate another MAC from the same key, if desired, via new calls to update and doFinal. (In order to reuse this Mac object with a different key, it must be reinitialized via a call to init(Key) or init(Key, AlgorithmParameterSpec).

Parameters
input:byte[]

data in bytes

Returns:byte[]

the MAC result.

Exceptions
IllegalStateException:
if this Mac has not been initialized.
getAlgorithmback to summary
public final String getAlgorithm()

Returns the algorithm name of this Mac object.

This is the same name that was specified in one of the getInstance calls that created this Mac object.

Returns:String

the algorithm name of this Mac object.

getInstanceback to summary
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException

Returns a Mac object that implements the specified MAC algorithm.

This method traverses the list of registered security providers, starting with the most preferred provider. A new Mac object encapsulating the MacSpi implementation from the first provider that supports the specified algorithm is returned.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Implementation Note

The JDK Reference Implementation additionally uses the jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different from the order of providers returned by Security.getProviders().

Parameters
algorithm:String

the standard name of the requested MAC algorithm. See the Mac section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

Returns:Mac

the new Mac object

Exceptions
NoSuchAlgorithmException:
if no Provider supports a MacSpi implementation for the specified algorithm
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
java.security.Provider
getInstanceback to summary
public static final Mac getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException

Returns a Mac object that implements the specified MAC algorithm.

A new Mac object encapsulating the MacSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Parameters
algorithm:String

the standard name of the requested MAC algorithm. See the Mac section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

provider:String

the name of the provider.

Returns:Mac

the new Mac object

Exceptions
NoSuchAlgorithmException:
if a MacSpi implementation for the specified algorithm is not available from the specified provider
NoSuchProviderException:
if the specified provider is not registered in the security provider list
IllegalArgumentException:
if the provider is null or empty
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
java.security.Provider
getInstanceback to summary
public static final Mac getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException

Returns a Mac object that implements the specified MAC algorithm.

A new Mac object encapsulating the MacSpi implementation from the specified provider is returned. Note that the specified provider does not have to be registered in the provider list.

Parameters
algorithm:String

the standard name of the requested MAC algorithm. See the Mac section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.

provider:Provider

the provider.

Returns:Mac

the new Mac object

Exceptions
NoSuchAlgorithmException:
if a MacSpi implementation for the specified algorithm is not available from the specified Provider object
IllegalArgumentException:
if the provider is null
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
java.security.Provider
getMacLengthback to summary
public final int getMacLength()

Returns the length of the MAC in bytes.

Returns:int

the MAC length in bytes.

getProviderback to summary
public final Provider getProvider()

Returns the provider of this Mac object.

Returns:Provider

the provider of this Mac object.

getProviderNameback to summary
private String getProviderName()
initback to summary
public final void init(Key key) throws InvalidKeyException

Initializes this Mac object with the given key.

Parameters
key:Key

the key.

Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this MAC.
initback to summary
public final void init(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException

Initializes this Mac object with the given key and algorithm parameters.

Parameters
key:Key

the key.

params:AlgorithmParameterSpec

the algorithm parameters.

Exceptions
InvalidKeyException:
if the given key is inappropriate for initializing this MAC.
InvalidAlgorithmParameterException:
if the given algorithm parameters are inappropriate for this MAC.
resetback to summary
public final void reset()

Resets this Mac object.

A call to this method resets this Mac object to the state it was in when previously initialized via a call to init(Key) or init(Key, AlgorithmParameterSpec). That is, the object is reset and available to generate another MAC from the same key, if desired, via new calls to update and doFinal. (In order to reuse this Mac object with a different key, it must be reinitialized via a call to init(Key) or init(Key, AlgorithmParameterSpec).

updateback to summary
public final void update(byte input) throws IllegalStateException

Processes the given byte.

Parameters
input:byte

the input byte to be processed.

Exceptions
IllegalStateException:
if this Mac has not been initialized.
updateback to summary
public final void update(byte[] input) throws IllegalStateException

Processes the given array of bytes.

Parameters
input:byte[]

the array of bytes to be processed.

Exceptions
IllegalStateException:
if this Mac has not been initialized.
updateback to summary
public final void update(byte[] input, int offset, int len) throws IllegalStateException

Processes the first len bytes in input, starting at offset inclusive.

Parameters
input:byte[]

the input buffer.

offset:int

the offset in input where the input starts.

len:int

the number of bytes to process.

Exceptions
IllegalStateException:
if this Mac has not been initialized.
updateback to summary
public final void update(ByteBuffer input)

Processes input.remaining() bytes in the ByteBuffer input, starting at input.position(). Upon return, the buffer's position will be equal to its limit; its limit will not have changed.

Parameters
input:ByteBuffer

the ByteBuffer

Exceptions
IllegalStateException:
if this Mac has not been initialized.
IllegalArgumentException:
if input is null
Since
1.5