Class DhPrivateKeySpec

java.lang.Object
zeroecho.core.alg.dh.DhPrivateKeySpec
All Implemented Interfaces:
Destroyable, AlgorithmKeySpec

public class DhPrivateKeySpec extends Object implements AlgorithmKeySpec, Destroyable
Key specification for a Diffie-Hellman private key encoded in PKCS#8 format.

This spec encapsulates the raw encoded private key bytes following the standard PKCS#8 structure. It is used with algorithms in the AlgorithmFamily.AGREEMENT family, allowing safe import/export of DH private keys across contexts.

Design

  • Destroyable: the internal byte array is defensively copied at construction and when returned by encoded(), and is cleared by destroy().
  • Encodable: supports marshaling to/from a PairSeq so keys can be serialized in human-readable or protocol-friendly formats.
  • Type-safe: used as a binding for asymmetric key builders within CryptoAlgorithm definitions.

Example

// Import a DH private key from encoded bytes
DhPrivateKeySpec spec = new DhPrivateKeySpec(pkcs8Bytes);
PrivateKey priv = session.keyBuilders().asymmetric().importPrivate("DH", spec);

// Marshal to a text-friendly representation
PairSeq ps = DhPrivateKeySpec.marshal(spec);

// Unmarshal back to spec
DhPrivateKeySpec parsed = DhPrivateKeySpec.unmarshal(ps);
Since:
1.0
  • Constructor Details

    • DhPrivateKeySpec

      public DhPrivateKeySpec(byte[] pkcs8)
      Creates a new specification from a PKCS#8 encoded DH private key.
      Parameters:
      pkcs8 - the encoded private key bytes; must not be null
      Throws:
      IllegalArgumentException - if pkcs8 is null
  • Method Details

    • encoded

      public byte[] encoded()
      Returns a clone of the encoded PKCS#8 bytes.
      Returns:
      a defensive copy of the PKCS#8 encoded DH private key
    • marshal

      public static PairSeq marshal(DhPrivateKeySpec spec)
      Marshals the given DH private key spec into a PairSeq.

      The sequence contains a type field with value "DH-PRIV" and a pkcs8.b64 field with the Base64 encoding of the key.

      Parameters:
      spec - the spec to encode
      Returns:
      a PairSeq representation of the spec
      Throws:
      NullPointerException - if spec is null
    • unmarshal

      public static DhPrivateKeySpec unmarshal(PairSeq p)
      Reconstructs a DhPrivateKeySpec from a PairSeq.

      The sequence must contain a pkcs8.b64 field with the Base64-encoded private key. If this field is missing, an IllegalArgumentException is thrown.

      Parameters:
      p - the sequence to decode
      Returns:
      a new spec containing the decoded PKCS#8 bytes
      Throws:
      IllegalArgumentException - if required fields are missing or malformed
    • destroy

      public void destroy()
      Specified by:
      destroy in interface Destroyable
    • isDestroyed

      public boolean isDestroyed()
      Specified by:
      isDestroyed in interface Destroyable