Class DhPublicKeySpec

java.lang.Object
zeroecho.core.alg.dh.DhPublicKeySpec
All Implemented Interfaces:
AlgorithmKeySpec

public class DhPublicKeySpec extends Object implements AlgorithmKeySpec
Key specification for a Diffie-Hellman public key encoded in X.509 format.

This spec carries the raw encoded public key bytes in the standard X.509 SubjectPublicKeyInfo form. It is intended for use with algorithms in the AlgorithmFamily.AGREEMENT family, allowing safe import/export of DH public keys across contexts.

Design

  • Immutable: the internal byte array is defensively copied at construction and when returned by encoded().
  • 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 public key from encoded bytes
DhPublicKeySpec spec = new DhPublicKeySpec(x509Bytes);
PublicKey pub = session.keyBuilders().asymmetric().importPublic("DH", spec);

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

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

    • DhPublicKeySpec

      public DhPublicKeySpec(byte[] key)
      Creates a new specification from an X.509 encoded DH public key.
      Parameters:
      key - the encoded public key bytes; must not be null
      Throws:
      NullPointerException - if key is null
  • Method Details

    • encoded

      public byte[] encoded()
      Returns a clone of the encoded X.509 bytes.
      Returns:
      a defensive copy of the X.509 encoded DH public key
    • marshal

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

      The sequence contains a type field with value "DH-PUB" and an x509.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 DhPublicKeySpec unmarshal(PairSeq p)
      Reconstructs a DhPublicKeySpec from a PairSeq.

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

      Parameters:
      p - the sequence to decode
      Returns:
      a new spec containing the decoded X.509 bytes
      Throws:
      IllegalArgumentException - if required fields are missing or malformed