Class NtruPublicKeySpec

java.lang.Object
zeroecho.core.alg.ntru.NtruPublicKeySpec
All Implemented Interfaces:
AlgorithmKeySpec

public final class NtruPublicKeySpec extends Object implements AlgorithmKeySpec
NtruPublicKeySpec holds an X.509 SubjectPublicKeyInfo encoded NTRU public key and supports a simple marshal/unmarshal form.

Instances are immutable. The byte array provided to the constructor is defensively copied and x509() returns a fresh clone on each call.

Usage

// Construct from existing X.509 bytes
byte[] x509Der = Files.readAllBytes(Path.of("ntru-public.der"));
NtruPublicKeySpec spec = new NtruPublicKeySpec(x509Der);

// Import into JCA (example provider alias shown)
KeyFactory kf = KeyFactory.getInstance("NTRU", "BCPQC");
PublicKey pk = kf.generatePublic(new X509EncodedKeySpec(spec.x509()));

// Marshal to a compact name-value form (Base64 without padding)
PairSeq serialized = NtruPublicKeySpec.marshal(spec);

// Later or elsewhere, reconstruct the spec
NtruPublicKeySpec restored = NtruPublicKeySpec.unmarshal(serialized);
Since:
1.0
  • Constructor Details

    • NtruPublicKeySpec

      public NtruPublicKeySpec(byte[] x509Der)
      Creates a new specification from X.509 SubjectPublicKeyInfo encoded bytes.

      The input array is not retained. A defensive copy is made and stored.

      Parameters:
      x509Der - X.509 DER bytes of the NTRU public key, must not be null
      Throws:
      NullPointerException - if x509Der is null
  • Method Details

    • x509

      public byte[] x509()
      Returns a copy of the X.509-encoded public key.

      The returned array is a fresh clone. Callers may modify it without affecting the spec.

      Returns:
      a new byte array containing the X.509 DER encoding
    • marshal

      public static PairSeq marshal(NtruPublicKeySpec spec)
      Serializes this specification into a simple name-value sequence.

      The output contains:

      • "type" set to "NtruPublicKey" for identification, and
      • "x509.b64" containing the X.509 bytes encoded with Base64 without padding.
      This format is suitable for lightweight persistence or interchange where a compact textual representation is preferred.
      Parameters:
      spec - the specification to serialize, must not be null
      Returns:
      a PairSeq carrying the serialized fields
      Throws:
      NullPointerException - if spec is null
    • unmarshal

      public static NtruPublicKeySpec unmarshal(PairSeq p)
      Deserializes a specification from a name-value sequence produced by marshal(NtruPublicKeySpec).

      Fields not recognized by this implementation are ignored to allow forward compatibility. The field "x509.b64" is required and must contain Base64 (no padding) of the X.509 bytes.

      Parameters:
      p - the sequence to read
      Returns:
      a new specification carrying the decoded X.509 bytes
      Throws:
      IllegalArgumentException - if the required "x509.b64" field is absent
      NullPointerException - if p is null
    • toString

      public String toString()
      Returns a short diagnostic string including the encoded length.
      Overrides:
      toString in class Object
      Returns:
      a human-readable form such as NtruPublicKeySpec[len=...]