Class DhKeyGenBuilder
java.lang.Object
zeroecho.core.alg.dh.DhKeyGenBuilder
- All Implemented Interfaces:
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
- If
DhSpec.params()is non-null, those DH parameters are used. - Otherwise, the builder creates parameters with
AlgorithmParameterGeneratorfor the requested bit length, then initializes aKeyPairGeneratorto produce the key pair.
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerateKeyPair(DhSpec spec) Generates a Diffie-Hellman key pair for the given specification.
-
Constructor Details
-
DhKeyGenBuilder
public DhKeyGenBuilder()Creates a stateless Diffie-Hellman key-pair generator.
-
-
Method Details
-
generateKeyPair
Generates a Diffie-Hellman key pair for the given specification.If
spec.params()is non-null, the containedDHParameterSpecis used directly. Otherwise, parameters are generated withAlgorithmParameterGeneratorusingspec.sizeBits(), and the resulting parameters are applied to aKeyPairGeneratorfor"DiffieHellman".Performance note: On some providers, ad-hoc parameter generation can be slow for large sizes. When possible, pass a
DhSpecthat supplies a known-goodDHParameterSpec.Example
KeyPair kp = new DhKeyGenBuilder().generateKeyPair(DhSpec.ffdhe3072());- Specified by:
generateKeyPairin interfaceAsymmetricKeyPairGenerator<DhSpec>- Parameters:
spec- the DH specification indicating either a concreteDHParameterSpecor 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
-