# Mac

public class **Mac** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
implements [Cloneable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Cloneable.html) 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., MD5 or SHA-1, in combination with a secret shared key. HMAC is
specified in RFC 2104.

Android provides the following `Mac` algorithms:

| Algorithm | Supported API Levels |
|---|---|
| DESMAC | 1-8 |
| DESMAC/CFB8 | 1-8 |
| DESedeMAC | 1-8 |
| DESedeMAC/CFB8 | 1-8 |
| DESedeMAC64 | 1-8 |
| DESwithISO9797 | 1-8 |
| HmacMD5 | 1+ |
| HmacSHA1 | 1+ |
| HmacSHA224 | 1-8,22+ |
| HmacSHA256 | 1+ |
| HmacSHA384 | 1+ |
| HmacSHA512 | 1+ |
| ISO9797ALG3MAC | 1-8 |
| PBEwithHmacSHA | 1+ |
| PBEwithHmacSHA1 | 1+ |
| PBEwithHmacSHA224 | 26+ |
| PBEwithHmacSHA256 | 26+ |
| PBEwithHmacSHA384 | 26+ |
| PBEwithHmacSHA512 | 26+ |

These algorithms are described in the [Mac section](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#Mac) of the Java Cryptography Architecture Standard Algorithm Name Documentation.

<br />

### Protected Constructor Summary

|---|---|
|   | [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#Mac(javax.crypto.MacSpi,%20java.security.Provider,%20java.lang.String))([MacSpi](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/MacSpi.html) macSpi, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm) Creates a MAC object. |

### Public Method Summary

|---|---|
| final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#clone())() Returns a clone if the provider implementation is cloneable. |
| final byte\[\] | [doFinal](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#doFinal(byte[]))(byte\[\] input) Processes the given array of bytes and finishes the MAC operation. |
| final void | [doFinal](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#doFinal(byte[],%20int))(byte\[\] output, int outOffset) Finishes the MAC operation. |
| final byte\[\] | [doFinal](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#doFinal())() Finishes the MAC operation. |
| final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [getAlgorithm](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#getAlgorithm())() Returns the algorithm name of this `Mac` object. |
| final static [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#getInstance(java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm) Returns a `Mac` object that implements the specified MAC algorithm. |
| final static [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#getInstance(java.lang.String,%20java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) provider) Returns a `Mac` object that implements the specified MAC algorithm. |
| final static [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html) | [getInstance](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#getInstance(java.lang.String,%20java.security.Provider))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider) Returns a `Mac` object that implements the specified MAC algorithm. |
| final int | [getMacLength](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#getMacLength())() Returns the length of the MAC in bytes. |
| final [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) | [getProvider](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#getProvider())() Returns the provider of this `Mac` object. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#init(java.security.Key,%20java.security.spec.AlgorithmParameterSpec))([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key, [AlgorithmParameterSpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/AlgorithmParameterSpec.html) params) Initializes this `Mac` object with the given key and algorithm parameters. |
| final void | [init](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#init(java.security.Key))([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key) Initializes this `Mac` object with the given key. |
| final void | [reset](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#reset())() Resets this `Mac` object. |
| final void | [update](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#update(byte[]))(byte\[\] input) Processes the given array of bytes. |
| final void | [update](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#update(byte[],%20int,%20int))(byte\[\] input, int offset, int len) Processes the first `len` bytes in `input`, starting at `offset` inclusive. |
| final void | [update](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#update(java.nio.ByteBuffer))([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) input) Processes `input.remaining()` bytes in the ByteBuffer `input`, starting at `input.position()`. |
| final void | [update](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html#update(byte))(byte input) Processes the given byte. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Protected Constructors

#### protected
**Mac**
([MacSpi](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/MacSpi.html) macSpi, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm)

Creates a MAC object.

##### Parameters

| macSpi | the delegate |
| provider | the provider |
| algorithm | the algorithm |
|---|---|

## Public Methods

#### public final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**clone**
()

Returns a clone if the provider implementation is cloneable.

##### Returns

- a clone if the provider implementation is cloneable.

##### Throws

| [CloneNotSupportedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CloneNotSupportedException.html) | if this is called on a delegate that does not support `Cloneable`. |
|---|---|

#### public final byte\[\]
**doFinal**
(byte\[\] input)

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 | data in bytes |
|---|---|

##### Returns

- the MAC result.

##### Throws

| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|

#### public final void
**doFinal**
(byte\[\] output, int outOffset)

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 | the buffer where the MAC result is stored |
| outOffset | the offset in `output` where the MAC is stored |
|---|---|

##### Throws

| [ShortBufferException](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/ShortBufferException.html) | if the given output buffer is too small to hold the result |
| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|

#### public final byte\[\]
**doFinal**
()

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

- the MAC result.

##### Throws

| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|

#### public final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**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

- the algorithm name of this `Mac` object.

#### public static final [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html)
**getInstance**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm)

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Security.html#getProviders()` method.

##### Parameters

| algorithm | the standard name of the requested MAC algorithm. See the Mac section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#Mac) for information about standard algorithm names. |
|---|---|

##### Returns

- the new `Mac` object.

##### Throws

| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if no Provider supports a MacSpi implementation for the specified algorithm. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html`

#### public static final [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html)
**getInstance**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) provider)

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Security.html#getProviders()` method.

##### Parameters

| algorithm | the standard name of the requested MAC algorithm. See the Mac section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#Mac) for information about standard algorithm names. |
| provider | the name of the provider. |
|---|---|

##### Returns

- the new `Mac` object.

##### Throws

| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if a MacSpi implementation for the specified algorithm is not available from the specified provider. |
| [NoSuchProviderException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchProviderException.html) | if the specified provider is not registered in the security provider list. |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the `provider` is null or empty. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html`

#### public static final [Mac](https://developers.google.com/j2objc/javadoc/jre/reference/javax/crypto/Mac.html)
**getInstance**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) algorithm, [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html) provider)

Returns a `Mac` object that implements the
specified MAC algorithm.

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

##### Parameters

| algorithm | the standard name of the requested MAC algorithm. See the Mac section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://developers.google.com/j2objc/javadoc/jre/technotes/guides/security/StandardNames.html#Mac) for information about standard algorithm names. |
| provider | the provider. |
|---|---|

##### Returns

- the new `Mac` object.

##### Throws

| [NoSuchAlgorithmException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/NoSuchAlgorithmException.html) | if a MacSpi implementation for the specified algorithm is not available from the specified Provider object. |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the `provider` is null. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html`

#### public final int
**getMacLength**
()

Returns the length of the MAC in bytes.

##### Returns

- the MAC length in bytes.

#### public final [Provider](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Provider.html)
**getProvider**
()

Returns the provider of this `Mac` object.

##### Returns

- the provider of this `Mac` object.

#### public final void
**init**
([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key, [AlgorithmParameterSpec](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/spec/AlgorithmParameterSpec.html) params)

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

##### Parameters

| key | the key. |
| params | the algorithm parameters. |
|---|---|

##### Throws

| [InvalidKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidKeyException.html) | if the given key is inappropriate for initializing this MAC. |
| [InvalidAlgorithmParameterException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidAlgorithmParameterException.html) | if the given algorithm parameters are inappropriate for this MAC. |
|---|---|

#### public final void
**init**
([Key](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/Key.html) key)

Initializes this `Mac` object with the given key.

##### Parameters

| key | the key. |
|---|---|

##### Throws

| [InvalidKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/security/InvalidKeyException.html) | if the given key is inappropriate for initializing this MAC. |
|---|---|

#### 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)`.

#### public final void
**update**
(byte\[\] input)

Processes the given array of bytes.

##### Parameters

| input | the array of bytes to be processed. |
|---|---|

##### Throws

| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|

#### public final void
**update**
(byte\[\] input, int offset, int len)

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

##### Parameters

| input | the input buffer. |
| offset | the offset in `input` where the input starts. |
| len | the number of bytes to process. |
|---|---|

##### Throws

| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|

#### public final void
**update**
([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) 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 | the ByteBuffer |
|---|---|

##### Throws

| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|

#### public final void
**update**
(byte input)

Processes the given byte.

##### Parameters

| input | the input byte to be processed. |
|---|---|

##### Throws

| [IllegalStateException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalStateException.html) | if this `Mac` has not been initialized. |
|---|---|