For the latest news and information visit The GNU Crypto project

## gnu.crypto.mac
Class HMac

    java.lang.Object
      |
      +--gnu.crypto.mac.BaseMac
            |
            +--gnu.crypto.mac.HMac

All Implemented Interfaces:
  java.lang.Cloneable, IMac

public class HMac
extends BaseMac

The implementation of the HMAC (Keyed-Hash Message Authentication Code).

HMAC can be used in combination with any iterated cryptographic hash function. HMAC also uses a secret key for calculation and verification of the message authentication values. The main goals behind this construction are

- To use, without modifications, available hash functions. In particular, hash functions that perform well in software, and for which code is freely and widely available.
- To preserve the original performance of the hash function without incurring a significant degradation.
- To use and handle keys in a simple way.
- To have a well understood cryptographic analysis of the strength of the authentication mechanism based on reasonable assumptions on the underlying hash function.
- To allow for easy replaceability of the underlying hash function in case that faster or more secure hash functions are found or required.

References:

1. RFC 2104HMAC: Keyed-Hashing for Message Authentication.
   H. Krawczyk, M. Bellare, and R. Canetti.

Field Summary
`protected int` `blockSize`
`protected byte[]` `ipad`
`protected IMessageDigest` `ipadHash`
`protected int` `macSize`
`protected IMessageDigest` `opadHash`
`static java.lang.String` `USE_WITH_PKCS5_V2`

Fields inherited from class gnu.crypto.mac.BaseMac
`name, truncatedSize, underlyingHash`

Fields inherited from interface gnu.crypto.mac.IMac
`MAC_KEY_MATERIAL, TRUNCATED_SIZE`

Constructor Summary
`protected` `HMac(IMessageDigest underlyingHash)` Trivial constructor for use by concrete subclasses.

Method Summary
`java.lang.Object` `clone()` Returns a clone copy of this instance.
`byte[]` `digest()` Completes the MAC by performing final operations such as padding and resetting the instance.
`void` `init(java.util.Map attributes)` Initialises the algorithm with designated attributes.
`void` `reset()` Resets the algorithm instance for re-initialisation and use with other characteristics.
`boolean` `selfTest()` A basic test.

Methods inherited from class gnu.crypto.mac.BaseMac
`macSize, name, update, update`

Methods inherited from class java.lang.Object
`equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### USE_WITH_PKCS5_V2

    public static final java.lang.String USE_WITH_PKCS5_V2

See Also:
  Constant Field Values

### macSize

    protected int macSize

### blockSize

    protected int blockSize

### ipadHash

    protected IMessageDigest ipadHash

### opadHash

    protected IMessageDigest opadHash

### ipad

    protected byte[] ipad

Constructor Detail

### HMac

    protected HMac(IMessageDigest underlyingHash)

  Trivial constructor for use by concrete subclasses.

Parameters:
  `underlyingHash` - the underlying hash algorithm instance.

Method Detail

### clone

    public java.lang.Object clone()

  Description copied from interface: `IMac`

  Returns a clone copy of this instance.

  Specified by:
    `clone` in interface `IMac`
  Specified by:
    `clone` in class `BaseMac`
  Returns:
    a clone copy of this instance.

### init

    public void init(java.util.Map attributes)
              throws java.security.InvalidKeyException,
                     java.lang.IllegalStateException

  Description copied from interface: `IMac`

  Initialises the algorithm with designated attributes. Permissible names and values are described in the class documentation above.

  Specified by:
    `init` in interface `IMac`
  Specified by:
    `init` in class `BaseMac`
  Parameters:
    `attributes` - a set of name-value pairs that describe the desired future instance behaviour.
  Throws:
    `java.security.InvalidKeyException` - if the key data is invalid.
    `java.lang.IllegalStateException` - if the instance is already initialised.
  See Also:
    `IMac.MAC_KEY_MATERIAL`

### reset

    public void reset()

  Description copied from interface: `IMac`

  Resets the algorithm instance for re-initialisation and use with other characteristics. This method always succeeds.

  Specified by:
    `reset` in interface `IMac`
  Overrides:
    `reset` in class `BaseMac`

### digest

    public byte[] digest()

  Description copied from interface: `IMac`

  Completes the MAC by performing final operations such as padding and resetting the instance.

  Specified by:
    `digest` in interface `IMac`
  Specified by:
    `digest` in class `BaseMac`
  Returns:
    the array of bytes representing the MAC value.

### selfTest

    public boolean selfTest()

  Description copied from interface: `IMac`

  A basic test. Ensures that the MAC of a pre-determined message is equal to a known pre-computed value.

  Specified by:
    `selfTest` in interface `IMac`
  Specified by:
    `selfTest` in class `BaseMac`
  Returns:
    `true` if the implementation passes a basic self-test. Returns `false` otherwise.

For the latest news and information visit The GNU Crypto project

Copyright © 2001, 2002, 2003, 2004 Free Software Foundation, Inc.