net.i2p.data

## Class KeysAndCert

- java.lang.Object
- - net.i2p.data.DataStructureImpl
  - - net.i2p.data.KeysAndCert

- All Implemented Interfaces:
    Serializable, DataStructure
  Direct Known Subclasses:
    Destination, RouterIdentity

      public class KeysAndCert
      extends DataStructureImpl

  Container for cryptographic keys and certificate as used by I2P identities.

  KeysAndCert provides the fundamental cryptographic identity components:

  - Public Key: Encryption key for encrypted communication
  - Signing Public Key: Key for verifying signatures and identity
  - Certificate: Optional metadata about the keys and identity
  - Cached Hash: Pre-computed SHA-256 hash for efficient identification

  Structure:

  - Public key (variable length, typically 256 bytes for ElGamal)
  - Signing public key (variable length, typically 128 bytes for DSA, 32 bytes for Ed25519)
  - Certificate (type + length + payload)
  - Optional padding for consistent serialization

  Usage:

  - Base Class: Extended by `Destination` and `RouterIdentity`
  - Identity: Forms the core of I2P cryptographic identities
  - Verification: Provides keys for signature verification
  - Encryption: Contains encryption key for secure communication

  Key Types:

  - Encryption: ElGamal 2048-bit (legacy) or ECIES X25519 (modern)
  - Signing: DSA-SHA1 (legacy), ECDSA-P256, or EdDSA-Ed25519 (modern)
  - Certificates: NULL, HIDDEN, SIGNED, MULTIPLE, or KEY types

  Immutability:

  - As of 0.9.9, instances are immutable after keys and certificate are set
  - Attempts to modify will throw `IllegalStateException`
  - Ensures thread safety and prevents accidental corruption

  History:

  - Implemented in 0.8.2 and retrofitted over existing Destination and RouterIdentity classes
  - No functional difference between Destination and RouterIdentity at this level
  - Provides unified interface for identity management across I2P
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Certificate` | `_certificate`
    `protected int` | `_paddingBlocks` If compressed, the padding size / 32, else 0
    `protected PublicKey` | `_publicKey`
    `protected SigningPublicKey` | `_signingKey`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `KeysAndCert()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Hash` | `calculateHash()` Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12.
    `protected static byte[]` | `combinePadding(byte[] pad1, byte[] pad2)` Combines two padding arrays.
    `boolean` | `equals(Object object)`
    `Certificate` | `getCertificate()`
    `EncType` | `getEncType()` Gets the encryption type from the certificate.
    `Hash` | `getHash()` Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12.
    `byte[]` | `getPadding()` Gets the padding bytes.
    `PublicKey` | `getPublicKey()` Valid for RouterIdentities.
    `SigningPublicKey` | `getSigningPublicKey()`
    `SigType` | `getSigType()` Gets the signature type from the certificate.
    `int` | `hashCode()` the signing key has enough randomness in it to use it by itself for speed
    `boolean` | `isCompressible()` Is there compressible padding?
    `void` | `readBytes(InputStream in)` Load up the current object with data from the given stream.
    `void` | `setCertificate(Certificate cert)` Sets the certificate.
    `void` | `setPadding(byte[] padding)` Sets the padding bytes.
    `void` | `setPublicKey(PublicKey key)` Sets the public key.
    `void` | `setSigningPublicKey(SigningPublicKey key)` Sets the signing public key.
    `String` | `toString()`
    `void` | `writeBytes(OutputStream out)` Write out the data structure to the stream, using the format defined in the I2P data structure specification.
    `protected int` | `writePaddingBytes(byte[] target, int off)` For Destination.writeBytes()

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

      `fromBase64, fromByteArray, read, toBase64, toByteArray`

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

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

- - ### Field Detail

    - #### _certificate

          protected Certificate _certificate

    - #### _paddingBlocks

          protected int _paddingBlocks

      If compressed, the padding size / 32, else 0

    - #### _publicKey

          protected PublicKey _publicKey

    - #### _signingKey

          protected SigningPublicKey _signingKey

  - ### Constructor Detail

    - #### KeysAndCert

          public KeysAndCert()

  - ### Method Detail

    - #### calculateHash

          public Hash calculateHash()

      Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12. Prior to that, returned null.
      Specified by:
        `calculateHash` in interface `DataStructure`
      Overrides:
        `calculateHash` in class `DataStructureImpl`
      Returns:
        SHA256 hash, or null if there were problems (data format or io errors)
      Throws:
        `IllegalStateException` - if keys and cert are not initialized

    - #### combinePadding

          protected static byte[] combinePadding(byte[] pad1,
                                                 byte[] pad2)

      Combines two padding arrays.
      Returns:
        null if both are null

    - #### equals

          public boolean equals(Object object)

      Overrides:
        `equals` in class `Object`

    - #### getCertificate

          public Certificate getCertificate()

    - #### getEncType

          public EncType getEncType()

      Gets the encryption type from the certificate.
      Returns:
        null if not set or unknown

    - #### getHash

          public Hash getHash()

      Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12. Prior to that, returned null.
      Throws:
        `IllegalStateException` - if keys and cert are not initialized

    - #### getPadding

          public byte[] getPadding()

      Gets the padding bytes.

    - #### getPublicKey

          public PublicKey getPublicKey()

      Valid for RouterIdentities. May contain random padding for Destinations.

    - #### getSigningPublicKey

          public SigningPublicKey getSigningPublicKey()

    - #### getSigType

          public SigType getSigType()

      Gets the signature type from the certificate.
      Returns:
        null if not set or unknown

    - #### hashCode

          public int hashCode()

      the signing key has enough randomness in it to use it by itself for speed
      Overrides:
        `hashCode` in class `Object`

    - #### isCompressible

          public boolean isCompressible()

      Is there compressible padding?

    - #### readBytes

          public void readBytes(InputStream in)
                         throws DataFormatException,
                                IOException

      Description copied from interface: `DataStructure`
      Load up the current object with data from the given stream. Data loaded this way must match the I2P data structure specification. Warning - many classes will throw IllegalStateException if data is already set.
      Parameters:
        `in` - stream to read from
      Throws:
        `IllegalStateException` - if data already set
        `DataFormatException` - if the data is improperly formatted
        `IOException` - if there was a problem reading the stream

    - #### setCertificate

          public void setCertificate(Certificate cert)

      Sets the certificate.
      Throws:
        `IllegalStateException` - if was already set

    - #### setPadding

          public void setPadding(byte[] padding)

      Sets the padding bytes.
      Throws:
        `IllegalStateException` - if was already set

    - #### setPublicKey

          public void setPublicKey(PublicKey key)

      Sets the public key.
      Throws:
        `IllegalStateException` - if was already set

    - #### setSigningPublicKey

          public void setSigningPublicKey(SigningPublicKey key)

      Sets the signing public key.
      Throws:
        `IllegalStateException` - if was already set

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### writeBytes

          public void writeBytes(OutputStream out)
                          throws DataFormatException,
                                 IOException

      Description copied from interface: `DataStructure`
      Write out the data structure to the stream, using the format defined in the I2P data structure specification.
      Parameters:
        `out` - stream to write to
      Throws:
        `DataFormatException` - if the data was incomplete or not yet ready to be written
        `IOException` - if there was a problem writing to the stream

    - #### writePaddingBytes

          protected int writePaddingBytes(byte[] target,
                                          int off)

      For Destination.writeBytes()
      Returns:
        the new offset