Class DhKeyGenBuilder

java.lang.Object
zeroecho.core.alg.dh.DhKeyGenBuilder
All Implemented Interfaces:
AsymmetricKeyPairGenerator<DhSpec>

public final class DhKeyGenBuilder extends Object implements AsymmetricKeyPairGenerator<DhSpec>

DH key pair builder

Builds Diffie-Hellman key pairs using the JCA providers available at runtime. This builder targets the JCA algorithm name "DiffieHellman" and accepts a DhSpec describing the desired parameter set.

How it works

Security note: Prefer named, vetted groups such as the RFC 7919 FFDHE sets exposed by DhSpec (for example DhSpec.ffdhe2048()) over ad-hoc parameter generation.

Thread-safety: Instances are stateless and can be shared across threads.

Example

DhKeyGenBuilder builder = new DhKeyGenBuilder();

// Use a standard FFDHE group
KeyPair kp1 = builder.generateKeyPair(DhSpec.ffdhe2048());

// Or request parameters by size when DhSpec.params() is null
DhSpec sized = DhSpec.ofBits(3072); // example factory returning size-only spec
KeyPair kp2 = builder.generateKeyPair(sized);
  • Constructor Details

    • DhKeyGenBuilder

      public DhKeyGenBuilder()
      Creates a stateless Diffie-Hellman key-pair generator.
  • Method Details

    • generateKeyPair

      public KeyPair generateKeyPair(DhSpec spec) throws GeneralSecurityException
      Generates a Diffie-Hellman key pair for the given specification.

      If spec.params() is non-null, the contained DHParameterSpec is used directly. Otherwise, parameters are generated with AlgorithmParameterGenerator using spec.sizeBits(), and the resulting parameters are applied to a KeyPairGenerator for "DiffieHellman".

      Performance note: On some providers, ad-hoc parameter generation can be slow for large sizes. When possible, pass a DhSpec that supplies a known-good DHParameterSpec.

      Example

      KeyPair kp = new DhKeyGenBuilder().generateKeyPair(DhSpec.ffdhe3072());
      
      Specified by:
      generateKeyPair in interface AsymmetricKeyPairGenerator<DhSpec>
      Parameters:
      spec - the DH specification indicating either a concrete DHParameterSpec or the target bit length for parameter generation; must not be null
      Returns:
      a newly generated DH KeyPair
      Throws:
      GeneralSecurityException - if the JCA provider does not support Diffie-Hellman, parameter generation fails, or key pair generation cannot be completed