net.i2p.crypto.elgamal

## Class KeyFactory

- java.lang.Object
- - java.security.KeyFactorySpi
  - - net.i2p.crypto.elgamal.KeyFactory

-     public final class KeyFactory
      extends KeyFactorySpi

  Java Security Provider KeyFactory implementation for ElGamal public-key cryptosystem. This class provides the bridge between ElGamal-specific key specifications and the standard Java Cryptography Architecture (JCA) KeyFactory interface. It enables conversion between different key representations and supports standard encoding formats for ElGamal operations within the I2P network.

  Supported Operations:

  - Key Generation: Create ElGamal keys from specifications
  - Key Conversion: Translate between different key formats
  - Standard Encoding: Support for PKCS#8 and X.509 formats
  - Key Specification: Extract key parameters from existing keys

  Supported Key Specifications:

  - `ElGamalPublicKeySpec` - ElGamal-specific public key specification
  - `ElGamalPrivateKeySpec` - ElGamal-specific private key specification
  - `PKCS8EncodedKeySpec` - Standard PKCS#8 private key encoding
  - `X509EncodedKeySpec` - Standard X.509 public key encoding

  Usage Example:

       // Create KeyFactory instance
       KeyFactory keyFactory = KeyFactory.getInstance("ElGamal", "I2P");

       // Generate private key from specification
       ElGamalParameterSpec params = new ElGamalParameterSpec(p, g);
       ElGamalPrivateKeySpec privSpec = new ElGamalPrivateKeySpec(privateKey, params);
       ElGamalPrivateKey privateKey = (ElGamalPrivateKey) keyFactory.generatePrivate(privSpec);

       // Convert to standard PKCS#8 format
       PKCS8EncodedKeySpec pkcs8Spec = keyFactory.getKeySpec(privateKey, PKCS8EncodedKeySpec.class);

  Thread Safety: This class is thread-safe and can be used concurrently by multiple threads. Key objects created by this factory are immutable and thread-safe.

  Security Considerations:

  - Private key material is handled securely during conversion
  - Standard encoding formats ensure interoperability with other providers
  - ElGamal parameters are validated during key creation
  See Also:
    `KeyFactory`, `ElGamalPublicKey`, `ElGamalPrivateKey`, `I2PProvider`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `KeyFactory()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected PrivateKey` | `engineGeneratePrivate(KeySpec keySpec)` Generates an ElGamal private key from the provided key specification.
    `protected PublicKey` | `engineGeneratePublic(KeySpec keySpec)` Generates an ElGamal public key from the provided key specification.
    `protected <T extends KeySpec> T` | `engineGetKeySpec(Key key, Class<T> keySpec)` Returns a specification (key material) of the given key object.
    `protected Key` | `engineTranslateKey(Key key)` Translates a key object from one provider to another.

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

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

- - ### Constructor Detail

    - #### KeyFactory

          public KeyFactory()

  - ### Method Detail

    - #### engineGeneratePrivate

          protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
                                              throws InvalidKeySpecException

      Generates an ElGamal private key from the provided key specification. This method converts various key specification formats into ElGamal private keys, supporting both ElGamal-specific specifications and standard PKCS#8 encoding.
      Specified by:
        `engineGeneratePrivate` in class `KeyFactorySpi`
      Parameters:
        `keySpec` - the key specification to convert
      Returns:
        the generated ElGamal private key
      Throws:
        `InvalidKeySpecException` - if the key specification is unsupported or malformed

    - #### engineGeneratePublic

          protected PublicKey engineGeneratePublic(KeySpec keySpec)
                                            throws InvalidKeySpecException

      Generates an ElGamal public key from the provided key specification. This method converts various key specification formats into ElGamal public keys, supporting both ElGamal-specific specifications and standard X.509 encoding.
      Specified by:
        `engineGeneratePublic` in class `KeyFactorySpi`
      Parameters:
        `keySpec` - the key specification to convert
      Returns:
        the generated ElGamal public key
      Throws:
        `InvalidKeySpecException` - if the key specification is unsupported or malformed

    - #### engineGetKeySpec

          protected <T extends KeySpec> T engineGetKeySpec(Key key,
                                                           Class<T> keySpec)
                                                    throws InvalidKeySpecException

      Returns a specification (key material) of the given key object. This method extracts the underlying key parameters from ElGamal keys and converts them into the requested specification format.
      Specified by:
        `engineGetKeySpec` in class `KeyFactorySpi`
      Type Parameters:
        `T` - the type of the returned key specification
      Parameters:
        `key` - the key to convert
        `keySpec` - the requested specification class
      Returns:
        the corresponding key specification
      Throws:
        `InvalidKeySpecException` - if the requested specification type is unsupported or the key cannot be converted

    - #### engineTranslateKey

          protected Key engineTranslateKey(Key key)
                                    throws InvalidKeyException

      Translates a key object from one provider to another. This method would allow conversion between ElGamal keys from different security providers, but currently no other ElGamal providers are known or supported in the I2P environment.
      Specified by:
        `engineTranslateKey` in class `KeyFactorySpi`
      Parameters:
        `key` - the key to translate
      Returns:
        the translated key
      Throws:
        `InvalidKeyException` - always, as no other ElGamal providers are supported