Class NtruPrivateKeySpec

java.lang.Object
zeroecho.core.alg.ntru.NtruPrivateKeySpec
All Implemented Interfaces:
Destroyable, AlgorithmKeySpec

public final class NtruPrivateKeySpec extends Object implements AlgorithmKeySpec, Destroyable
NtruPrivateKeySpec holds a PKCS#8-encoded NTRU private key and supports a simple marshal/unmarshal form.

The byte array provided to the constructor is defensively copied and pkcs8() returns a fresh clone on each call. Access and destruction are synchronized.

Usage

// Construct from existing PKCS#8 bytes
byte[] pkcs8Der = Files.readAllBytes(Path.of("ntru-private.der"));
NtruPrivateKeySpec spec = new NtruPrivateKeySpec(pkcs8Der);

// Import into JCA (example provider alias shown)
KeyFactory kf = KeyFactory.getInstance("NTRU", "BCPQC");
PrivateKey sk = kf.generatePrivate(new PKCS8EncodedKeySpec(spec.pkcs8()));

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

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

    • NtruPrivateKeySpec

      public NtruPrivateKeySpec(byte[] pkcs8Der)
      Creates a new specification from PKCS#8-encoded bytes.

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

      Parameters:
      pkcs8Der - PKCS#8 DER bytes of the NTRU private key, must not be null
      Throws:
      NullPointerException - if pkcs8Der is null
  • Method Details

    • pkcs8

      public byte[] pkcs8()
      Returns a copy of the PKCS#8-encoded private key.

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

      Returns:
      a new byte array containing the PKCS#8 DER encoding
    • marshal

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

      The output contains:

      • "type" set to "NtruPrivateKey" for identification, and
      • "pkcs8.b64" containing the PKCS#8 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 NtruPrivateKeySpec unmarshal(PairSeq p)
      Deserializes a specification from a name-value sequence produced by marshal(NtruPrivateKeySpec).

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

      Parameters:
      p - the sequence to read
      Returns:
      a new specification carrying the decoded PKCS#8 bytes
      Throws:
      IllegalArgumentException - if the required "pkcs8.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 NtruPrivateKeySpec[len=...]}
    • destroy

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

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