Class XdhPublicKeySpec

java.lang.Object
zeroecho.core.alg.xdh.XdhPublicKeySpec
All Implemented Interfaces:
AlgorithmKeySpec

public class XdhPublicKeySpec extends Object implements AlgorithmKeySpec
Key specification for an XDH (elliptic curve Diffie-Hellman) public key.

This class encapsulates the X.509-encoded representation of a public key used in modern Diffie-Hellman style agreements such as X25519 or X448. It provides safe construction, serialization, and deserialization utilities suitable for persistence, transmission, or integration with CryptoAlgorithm key builders.

Design goals

  • Encapsulation: the encoded key bytes are cloned defensively on input and output to prevent accidental modification.
  • Interoperability: supports round-trip conversion to/from PairSeq with base64 encoding.
  • Validation: throws explicit exceptions when mandatory fields are missing.

Usage

// Construct from an X.509-encoded public key
byte[] encoded = ...; // obtained from a certificate or peer
XdhPublicKeySpec spec = new XdhPublicKeySpec(encoded);

// Marshal to a serializable structure
PairSeq p = XdhPublicKeySpec.marshal(spec);

// Unmarshal back from serialized form
XdhPublicKeySpec spec2 = XdhPublicKeySpec.unmarshal(p);

assert Arrays.equals(spec.encoded(), spec2.encoded());
Since:
1.0
  • Constructor Details

    • XdhPublicKeySpec

      public XdhPublicKeySpec(byte[] key)
      Constructs a new specification from the given X.509-encoded public key.
      Parameters:
      key - the raw encoded key bytes, must not be null
      Throws:
      NullPointerException - if key is null
  • Method Details

    • encoded

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

      public static PairSeq marshal(XdhPublicKeySpec spec)
      Serializes the given key specification into a PairSeq with base64 encoding.

      The output contains two entries:

      • type = "XDH-PUB"
      • x509.b64 = <base64-encoded key>
      Parameters:
      spec - the specification to marshal
      Returns:
      a pair sequence containing the encoded key
      Throws:
      NullPointerException - if spec is null
    • unmarshal

      public static XdhPublicKeySpec unmarshal(PairSeq p)
      Deserializes a specification from the given PairSeq.

      The sequence must contain an entry x509.b64 with a base64-encoded key. Additional entries are ignored.

      Parameters:
      p - the pair sequence containing serialized fields
      Returns:
      a reconstructed XdhPublicKeySpec
      Throws:
      IllegalArgumentException - if x509.b64 is missing or cannot be decoded