net.i2p.crypto

## Class HMAC256Generator

- java.lang.Object
- - net.i2p.crypto.HMACGenerator
  - - net.i2p.crypto.HMAC256Generator

-     public final class HMAC256Generator
      extends HMACGenerator

  HMAC-SHA256 message authentication code generator for I2P cryptographic operations. This class provides HMAC-SHA256 calculation using the standard JCA Mac interface, ensuring compatibility with `javax.crypto.Mac.getInstance("HmacSHA256")`. It offers both one-shot calculation and streaming operations for different use cases.

  Key features:

  - HMAC-SHA256 algorithm implementation for message authentication
  - Thread-safe operation with Mac instance pooling
  - Optimized for both small and large data processing
  - Integration with I2P's session key management

  Compatibility Note: As of 0.9.12, this class uses the standard javax.crypto.Mac implementation for improved security and compatibility.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `(package private) static class` | `HMAC256Generator.HMACKey` Performance-optimized SecretKey implementation for HMAC operations.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `HMAC256Generator(I2PAppContext context)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `(package private) Mac` | `acquire()` Package private for HKDF.
    `void` | `calculate(byte[] key, byte[] data, int offset, int length, byte[] target, int targetOffset)` Calculate the HMAC of the data with the given key.
    `void` | `calculate(SessionKey key, byte[] data, int offset, int length, byte[] target, int targetOffset)` Calculate the HMAC of the data with the given key.
    `(package private) void` | `release(Mac mac)` Mac will be reset and initialized with a zero key.
    `boolean` | `verify(SessionKey key, byte[] curData, int curOffset, int curLength, byte[] origMAC, int origMACOffset, int origMACLength)` Verify the MAC inline, reducing some unnecessary memory churn.

    - ### Methods inherited from class net.i2p.crypto.HMACGenerator

      `acquireTmp, releaseTmp`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### HMAC256Generator

          public HMAC256Generator(I2PAppContext context)

      Parameters:
        `context` - unused

  - ### Method Detail

    - #### acquire

          Mac acquire()

      Package private for HKDF.
      Returns:
        cached or Mac.getInstance("HmacSHA256")

    - #### calculate

          public void calculate(byte[] key,
                                byte[] data,
                                int offset,
                                int length,
                                byte[] target,
                                int targetOffset)

      Calculate the HMAC of the data with the given key. Outputs 32 bytes to target starting at targetOffset.
      Parameters:
        `key` - first 32 bytes used as the key
      Throws:
        `UnsupportedOperationException` - if the JVM does not support it
        `IllegalArgumentException` - for bad key or target too small

    - #### calculate

          public void calculate(SessionKey key,
                                byte[] data,
                                int offset,
                                int length,
                                byte[] target,
                                int targetOffset)

      Calculate the HMAC of the data with the given key. Outputs 32 bytes to target starting at targetOffset.
      Specified by:
        `calculate` in class `HMACGenerator`
      Parameters:
        `key` - the session key
        `data` - the data to HMAC
        `offset` - the starting offset in data
        `length` - the length of data to HMAC
        `target` - out parameter the first 16 bytes contain the HMAC, the last 16 bytes are zero
        `targetOffset` - offset into target to put the hmac
      Throws:
        `UnsupportedOperationException` - if the JVM does not support it
        `IllegalArgumentException` - for bad key or target too small

    - #### release

          void release(Mac mac)

      Mac will be reset and initialized with a zero key. Package private for HKDF.

    - #### verify

          public boolean verify(SessionKey key,
                                byte[] curData,
                                int curOffset,
                                int curLength,
                                byte[] origMAC,
                                int origMACOffset,
                                int origMACLength)

      Verify the MAC inline, reducing some unnecessary memory churn.
      Specified by:
        `verify` in class `HMACGenerator`
      Parameters:
        `key` - session key to verify the MAC with
        `curData` - MAC to verify
        `curOffset` - index into curData to MAC
        `curLength` - how much data in curData do we want to run the HMAC over
        `origMAC` - what do we expect the MAC of curData to equal
        `origMACOffset` - index into origMAC
        `origMACLength` - how much of the MAC do we want to verify, use 32 for HMAC256