Class XdhPrivateKeySpec

java.lang.Object
zeroecho.core.alg.xdh.XdhPrivateKeySpec
All Implemented Interfaces:
Destroyable, AlgorithmKeySpec

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

This class encapsulates the PKCS#8-encoded representation of a private 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 a PKCS#8-encoded private key
byte[] encoded = ...; // obtained from key generation or storage
XdhPrivateKeySpec spec = new XdhPrivateKeySpec(encoded);

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

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

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

    • XdhPrivateKeySpec

      public XdhPrivateKeySpec(byte[] pkcs8)
      Constructs a new specification from the given PKCS#8-encoded private key.
      Parameters:
      pkcs8 - the raw encoded key bytes, must not be null
      Throws:
      IllegalArgumentException - if pkcs8 is null
  • Method Details

    • encoded

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

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

      The output contains two entries:

      • type = "XDH-PRIV"
      • pkcs8.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 XdhPrivateKeySpec unmarshal(PairSeq p)
      Deserializes a specification from the given PairSeq.

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

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

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

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