Class NtruPrivateKeySpec
java.lang.Object
zeroecho.core.alg.ntru.NtruPrivateKeySpec
- All Implemented Interfaces:
Destroyable, AlgorithmKeySpec
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 Summary
ConstructorsConstructorDescriptionNtruPrivateKeySpec(byte[] pkcs8Der) Creates a new specification from PKCS#8-encoded bytes. -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()booleanstatic PairSeqmarshal(NtruPrivateKeySpec spec) Serializes this specification into a simple name-value sequence.byte[]pkcs8()Returns a copy of the PKCS#8-encoded private key.toString()Returns a short diagnostic string including the encoded length.static NtruPrivateKeySpecDeserializes a specification from a name-value sequence produced bymarshal(NtruPrivateKeySpec).
-
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- ifpkcs8Deris 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
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.
- Parameters:
spec- the specification to serialize, must not be null- Returns:
- a
PairSeqcarrying the serialized fields - Throws:
NullPointerException- ifspecis null
-
unmarshal
Deserializes a specification from a name-value sequence produced bymarshal(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 absentNullPointerException- ifpis null
-
toString
-
destroy
public void destroy()- Specified by:
destroyin interfaceDestroyable
-
isDestroyed
public boolean isDestroyed()- Specified by:
isDestroyedin interfaceDestroyable
-