net.i2p.data

## Class SessionKey

- java.lang.Object
- - net.i2p.data.SimpleDataStructure
  - - net.i2p.data.SessionKey

- All Implemented Interfaces:
    DataStructure
  Direct Known Subclasses:
    SessionKeyAndNonce

      public class SessionKey
      extends SimpleDataStructure

  Cryptographic session key for symmetric encryption in I2P.

  SessionKey provides symmetric encryption capabilities for I2P communications:

  - Fixed Size: 32-byte key suitable for modern symmetric algorithms
  - Session-Based: Temporary keys for message encryption/decryption
  - Algorithm Agnostic: Can be used with various symmetric ciphers
  - Efficient Storage: Optimized for high-frequency operations

  Key Generation:

  - Random Creation: Use `I2PAppContext.keyGenerator().generateSessionKey()`
  - Cryptographically Secure: Generated using cryptographically strong randomness
  - Factory Methods: Static creation methods for convenience
  - Invalid Key: Pre-defined all-zero key for special cases

  Usage Patterns:

  - Message Encryption: Encrypt I2NP messages and I2CP data
  - Tunnel Encryption: Protect tunnel payload contents
  - Session Establishment: Derive from key exchange protocols
  - Temporary Storage: Short-lived keys for enhanced security

  Security Considerations:

  - Key Protection: Never expose session keys to unauthorized parties
  - Secure Generation: Use cryptographically secure random sources
  - Key Rotation: Change keys regularly to limit exposure
  - Memory Security: Zeroize memory when keys are no longer needed

  Performance Features:

  - Prepared Keys: Cache for algorithm-specific prepared keys
  - Efficient Storage: Optimized byte array representation
  - Fast Operations: Quick access and comparison methods
  - Memory Management: Careful allocation and cleanup

  Integration:

  - I2P Client: Used for end-to-end encryption in I2CP
  - Router Operations: Tunnel and message encryption
  - Garlic Encryption: Multi-layer message protection
  - ElGamal Integration: Combined with asymmetric encryption for key exchange

  Constants:

  - `KEYSIZE_BYTES` - Standard 32-byte key length
  - `INVALID_KEY` - All-zero key for invalid/placeholder use

  Best Practices:

  - Unique Sessions: Use different keys for different communication sessions
  - Limited Lifetime: Replace keys after reasonable time period
  - Secure Disposal: Clear key data when session ends
  - Algorithm Selection: Use appropriate symmetric cipher for key size

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static SessionKey` | `INVALID_KEY` A key with all zeroes in the data
    `static int` | `KEYSIZE_BYTES`

    - ### Fields inherited from class net.i2p.data.SimpleDataStructure

      `_data`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SessionKey()`
    `SessionKey(byte[] data)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Object` | `getPreparedKey()` retrieve an internal representation of the session key, as known by the AES engine used.
    `int` | `length()` The legal length of the byte array in this data structure
    `void` | `setData(byte[] data)` Sets the data.
    `void` | `setPreparedKey(Object obj)`

    - ### Methods inherited from class net.i2p.data.SimpleDataStructure

      `calculateHash, equals, fromBase64, fromByteArray, getData, hashCode, read, readBytes, toBase64, toByteArray, toString, writeBytes`

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

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### INVALID_KEY

          public static final SessionKey INVALID_KEY

      A key with all zeroes in the data

    - #### KEYSIZE_BYTES

          public static final int KEYSIZE_BYTES

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### SessionKey

          public SessionKey()

    - #### SessionKey

          public SessionKey(byte[] data)

  - ### Method Detail

    - #### getPreparedKey

          public Object getPreparedKey()

      retrieve an internal representation of the session key, as known by the AES engine used. this can be reused safely

    - #### length

          public int length()

      Description copied from class: `SimpleDataStructure`
      The legal length of the byte array in this data structure
      Specified by:
        `length` in class `SimpleDataStructure`

    - #### setData

          public void setData(byte[] data)

      Sets the data.
      Overrides:
        `setData` in class `SimpleDataStructure`
      Parameters:
        `data` - 32 bytes, or null
      Throws:
        `IllegalArgumentException` - if data is not the legal number of bytes (but null is ok)
        `RuntimeException` - if data already set.

    - #### setPreparedKey

          public void setPreparedKey(Object obj)