java.lang.Object
net.i2p.data.DataStructureImpl
net.i2p.data.KeysAndCert
- All Implemented Interfaces:
Serializable, DataStructure
- Direct Known Subclasses:
Destination, RouterIdentity
Container for cryptographic keys and certificate as used by I2P identities.
KeysAndCert provides the fundamental cryptographic identity components:
- Public Key: Encryption key for encrypted communication
- Signing Public Key: Key for verifying signatures and identity
- Certificate: Optional metadata about the keys and identity
- Cached Hash: Pre-computed SHA-256 hash for efficient identification
Structure:
- Public key (variable length, typically 256 bytes for ElGamal)
- Signing public key (variable length, typically 128 bytes for DSA, 32 bytes for Ed25519)
- Certificate (type + length + payload)
- Optional padding for consistent serialization
Usage:
- Base Class: Extended by
DestinationandRouterIdentity - Identity: Forms the core of I2P cryptographic identities
- Verification: Provides keys for signature verification
- Encryption: Contains encryption key for secure communication
Key Types:
- Encryption: ElGamal 2048-bit (legacy) or ECIES X25519 (modern)
- Signing: DSA-SHA1 (legacy), ECDSA-P256, or EdDSA-Ed25519 (modern)
- Certificates: NULL, HIDDEN, SIGNED, MULTIPLE, or KEY types
Immutability:
- As of 0.9.9, instances are immutable after keys and certificate are set
- Attempts to modify will throw
IllegalStateException - Ensures thread safety and prevents accidental corruption
History:
- Implemented in 0.8.2 and retrofitted over existing Destination and RouterIdentity classes
- No functional difference between Destination and RouterIdentity at this level
- Provides unified interface for identity management across I2P
- See Also:
Field Summary
FieldsModifier and TypeFieldDescriptionprivate HashCached calculated hash value.private KeyCertificateCached key certificate, created lazily from_certificatewhen that is a plain CERTIFICATE_TYPE_KEY Certificate.protected CertificateCertificate specifying key types and metadata.private static final LogLogger instance.private byte[]If compressed, 32 bytes only.protected intIf compressed, the padding size / 32, else 0.protected PublicKeyPublic encryption key.protected SigningPublicKeyPublic signing key.private static final intHash bytes needed for the 8-char b32 hash prefix intoString().private static final intCompressed padding block length in bytes.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionThrows IllegalStateException if keys and cert are not initialized, as of 0.9.12.protected static byte[]combinePadding(byte[] pad1, byte[] pad2) Combines two padding arrays.private voidThis only does the padding, does not compress the unused 256 byte LS public key.booleanCompares this KeysAndCert with another object for equality.Certificate for this identity.Encryption type from the certificate.getHash()Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12.private KeyCertificategetKeyCertificate(Certificate cert) Up-convert the certificate to a KeyCertificate, caching the result.byte[]Padding bytes for key alignment.Valid for RouterIdentities.Signing public key for this identity.Signature type from the certificate.inthashCode()The signing key has enough randomness in it to use it by itself for speed.booleanIs there compressible padding?voidreadBytes(InputStream in) Reads the keys and certificate from an input stream.voidsetCertificate(Certificate cert) Certificate to set.voidsetPadding(byte[] padding) Padding bytes to set.voidsetPublicKey(PublicKey key) Public key to set.voidSigning public key to set.toString()Returns a string representation of this KeysAndCert.voidwriteBytes(OutputStream out) Writes the keys and certificate to an output stream.protected intwritePaddingBytes(byte[] target, int off) ForDestination.writeBytes().Methods inherited from class DataStructureImpl
fromBase64, fromByteArray, read, toBase64, toByteArrayModifier and TypeMethodDescriptionvoidfromBase64(String data) Load the structure from the provided base64 string.voidfromByteArray(byte[] data) Loads structure from the data providedprotected intread(InputStream in, byte[] target) Repeated reads until buffer is full or IOException is thrown.toBase64()Render the structure into modified base 64 notation.byte[]Convert the structure to a byte array.
Field Details
_publicKey
Public encryption key._signingKey
Public signing key._certificate
Certificate specifying key types and metadata.__calculatedHash
Cached calculated hash value.__keyCertificate
Cached key certificate, created lazily from_certificatewhen that is a plain CERTIFICATE_TYPE_KEY Certificate. null when there is nothing to cache. Volatile so a racy double-creation is harmless._padding
private byte[] _paddingIf compressed, 32 bytes only._paddingBlocks
protected int _paddingBlocksIf compressed, the padding size / 32, else 0.PAD_COMP_LEN
private static final int PAD_COMP_LENCompressed padding block length in bytes.- See Also:
B32_PREFIX_BYTES
private static final int B32_PREFIX_BYTESHash bytes needed for the 8-char b32 hash prefix intoString(). 8 b32 chars carry 40 bits, and 5 bytes is the smallest input that produces a full 8 chars.- See Also:
_log
Logger instance.
Constructor Details
KeysAndCert
public KeysAndCert()Constructor. An empty key and certificate set, ready forreadBytes(InputStream)or for the subclasses to populate field by field.
Method Details
getCertificate
Certificate for this identity.- Returns:
- the certificate authorizing this identity's keys
setCertificate
Certificate to set.- Parameters:
cert- the certificate to store, which also supplies the signature and encryption types- Throws:
IllegalStateException- if was already set
getSigType
getEncType
getKeyCertificate
Up-convert the certificate to a KeyCertificate, caching the result. The cache avoids a new KeyCertificate allocation on every identity-type query for identities holding a plain CERTIFICATE_TYPE_KEY Certificate (KeyCertificate.toKeyCertificate()already returns itself).- Parameters:
cert- the non-null certificate- Returns:
- the key certificate, or null if the certificate is not of type CERTIFICATE_TYPE_KEY or its payload is too short
getPublicKey
Valid for RouterIdentities. May contain random padding for Destinations.- Returns:
- the public key
setPublicKey
Public key to set.- Parameters:
key- the public key to store- Throws:
IllegalStateException- if was already set
getSigningPublicKey
Signing public key for this identity.- Returns:
- the signing public key
setSigningPublicKey
Signing public key to set.- Parameters:
key- the signing public key to store- Throws:
IllegalStateException- if was already set
getPadding
public byte[] getPadding()Padding bytes for key alignment.- Returns:
- the stored padding, null when none was set, otherwise the compressed form when more than one block was stored
setPadding
public void setPadding(byte[] padding) Padding bytes to set.- Parameters:
padding- the padding to store, whose length must be 32 or a multiple of 32 forisCompressible()to become true- Throws:
IllegalStateException- if was already set
isCompressible
public boolean isCompressible()Is there compressible padding?- Returns:
- true when more than one padding block was stored, so the padding can be recompressed on write
readBytes
Reads the keys and certificate from an input stream.- Parameters:
in- stream to read from- Throws:
IllegalStateException- if data already setDataFormatException- if the data is improperly formattedIOException- if there was a problem reading the stream
combinePadding
protected static byte[] combinePadding(byte[] pad1, byte[] pad2) Combines two padding arrays.- Parameters:
pad1- the first padding array, whose bytes lead the resultpad2- the second padding array, whose bytes follow those of pad1- Returns:
- null if both are null
compressPadding
private void compressPadding()This only does the padding, does not compress the unused 256 byte LS public key. Savings is 288 bytes for RI and 64 bytes for LS.writePaddingBytes
protected int writePaddingBytes(byte[] target, int off) ForDestination.writeBytes().- Parameters:
target- the buffer receiving the paddingoff- the offset in target at which to start writing- Returns:
- the new offset
writeBytes
Writes the keys and certificate to an output stream.- Parameters:
out- stream to write to- Throws:
DataFormatException- if the data was incomplete or not yet ready to be writtenIOException- if there was a problem writing to the stream
equals
hashCode
toString
calculateHash
Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12. Prior to that, returned null.- Specified by:
calculateHashin interfaceDataStructure- Overrides:
calculateHashin classDataStructureImpl- Returns:
- SHA256 hash, or null if there were problems (data format or io errors)
- Throws:
IllegalStateException- if keys and cert are not initialized
getHash
Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12. Prior to that, returned null.- Returns:
- the SHA-256 of the serialized keys and cert, cached after the first calculation
- Throws:
IllegalStateException- if keys and cert are not initialized