1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

Add documentation to PGPainless class

This commit is contained in:
Paul Schaub 2025-05-07 11:29:06 +02:00
parent 9ed53308c6
commit 9856aa43c4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -31,6 +31,17 @@ import org.pgpainless.key.parsing.KeyRingReader
import org.pgpainless.policy.Policy import org.pgpainless.policy.Policy
import org.pgpainless.util.ArmorUtils import org.pgpainless.util.ArmorUtils
/**
* Main entry point to the PGPainless OpenPGP API. Historically, this class was used through static
* factory methods only, and configuration was done using the Singleton pattern. However, now it is
* recommended to instantiate the API and apply configuration on a per-instance manner. The benefit
* of this being that you can have multiple differently configured instances at the same time.
*
* @param implementation OpenPGP Implementation - either BCs lightweight
* [org.bouncycastle.openpgp.api.bc.BcOpenPGPImplementation] or JCAs
* [org.bouncycastle.openpgp.api.jcajce.JcaOpenPGPImplementation].
* @param algorithmPolicy policy, deciding acceptable algorithms
*/
class PGPainless( class PGPainless(
val implementation: OpenPGPImplementation = OpenPGPImplementation.getInstance(), val implementation: OpenPGPImplementation = OpenPGPImplementation.getInstance(),
val algorithmPolicy: Policy = Policy() val algorithmPolicy: Policy = Policy()
@ -48,16 +59,36 @@ class PGPainless(
api = BcOpenPGPApi(implementation) api = BcOpenPGPApi(implementation)
} }
/**
* Generate a new [OpenPGPKey] from predefined templates.
*
* @param version [OpenPGPKeyVersion], defaults to [OpenPGPKeyVersion.v4]
* @param creationTime of the key, defaults to now
* @return [KeyRingTemplates] api
*/
@JvmOverloads @JvmOverloads
fun generateKey( fun generateKey(
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4, version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
creationTime: Date = Date() creationTime: Date = Date()
): KeyRingTemplates = KeyRingTemplates(version, creationTime, this) ): KeyRingTemplates = KeyRingTemplates(version, creationTime, this)
/**
* Build a fresh, custom [OpenPGPKey] using PGPainless' API.
*
* @param version [OpenPGPKeyVersion], defaults to [OpenPGPKeyVersion.v4]
* @return [KeyRingBuilder] api
*/
@JvmOverloads @JvmOverloads
fun buildKey(version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4): KeyRingBuilder = fun buildKey(version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4): KeyRingBuilder =
KeyRingBuilder(version, this) KeyRingBuilder(version, this)
/**
* Build a fresh, custom [OpenPGPKey] using BCs new API.
*
* @param version [OpenPGPKeyVersion], defaults to [OpenPGPKeyVersion.v4]
* @param creationTime creation time of the key, defaults to now
* @return [OpenPGPKeyGenerator] api
*/
@JvmOverloads @JvmOverloads
fun _buildKey( fun _buildKey(
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4, version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
@ -81,18 +112,52 @@ class PGPainless(
fun inspect(keyOrCertificate: OpenPGPCertificate, referenceTime: Date = Date()): KeyRingInfo = fun inspect(keyOrCertificate: OpenPGPCertificate, referenceTime: Date = Date()): KeyRingInfo =
KeyRingInfo(keyOrCertificate, this, referenceTime) KeyRingInfo(keyOrCertificate, this, referenceTime)
/**
* Modify an [OpenPGPKey], adding new components and signatures. This API can be used to add new
* subkeys, user-ids or user-attributes to the key, extend or alter its expiration time, revoke
* individual components of the entire certificate, etc.
*
* @param key key to modify
* @param referenceTime timestamp for modifications
* @return [SecretKeyRingEditor] api
*/
@JvmOverloads @JvmOverloads
fun modify(key: OpenPGPKey, referenceTime: Date = Date()): SecretKeyRingEditor = fun modify(key: OpenPGPKey, referenceTime: Date = Date()): SecretKeyRingEditor =
SecretKeyRingEditor(key, this, referenceTime) SecretKeyRingEditor(key, this, referenceTime)
/**
* Parse [OpenPGPKey]/[OpenPGPCertificate] material from binary or ASCII armored encoding.
*
* @return [OpenPGPKeyReader] api
*/
fun readKey(): OpenPGPKeyReader = api.readKeyOrCertificate() fun readKey(): OpenPGPKeyReader = api.readKeyOrCertificate()
/**
* Convert a [PGPSecretKeyRing] into an [OpenPGPKey].
*
* @param secretKeyRing mid-level API [PGPSecretKeyRing] object
* @return high-level API [OpenPGPKey] object
*/
fun toKey(secretKeyRing: PGPSecretKeyRing): OpenPGPKey = fun toKey(secretKeyRing: PGPSecretKeyRing): OpenPGPKey =
OpenPGPKey(secretKeyRing, implementation) OpenPGPKey(secretKeyRing, implementation)
/**
* Convert a [PGPPublicKeyRing] into an [OpenPGPCertificate].
*
* @param certificate mid-level API [PGPSecretKeyRing] object
* @return high-level API [OpenPGPCertificate] object
*/
fun toCertificate(certificate: PGPPublicKeyRing): OpenPGPCertificate = fun toCertificate(certificate: PGPPublicKeyRing): OpenPGPCertificate =
OpenPGPCertificate(certificate, implementation) OpenPGPCertificate(certificate, implementation)
/**
* Depending on the type, convert either a [PGPSecretKeyRing] into an [OpenPGPKey] or a
* [PGPPublicKeyRing] into an [OpenPGPCertificate].
*
* @param keyOrCertificate [PGPKeyRing], either [PGPSecretKeyRing] or [PGPPublicKeyRing]
* @return depending on the type of [keyOrCertificate], either an [OpenPGPKey] or
* [OpenPGPCertificate]
*/
fun toKeyOrCertificate(keyOrCertificate: PGPKeyRing): OpenPGPCertificate = fun toKeyOrCertificate(keyOrCertificate: PGPKeyRing): OpenPGPCertificate =
when (keyOrCertificate) { when (keyOrCertificate) {
is PGPSecretKeyRing -> toKey(keyOrCertificate) is PGPSecretKeyRing -> toKey(keyOrCertificate)
@ -102,6 +167,15 @@ class PGPainless(
"Unexpected PGPKeyRing subclass: ${keyOrCertificate.javaClass.name}") "Unexpected PGPKeyRing subclass: ${keyOrCertificate.javaClass.name}")
} }
/**
* Merge two copies of an [OpenPGPCertificate] into a single copy. This method can be used to
* import new third-party signatures into a certificate.
*
* @param originalCopy local copy of the certificate
* @param updatedCopy copy of the same certificate, potentially carrying new signatures and
* components
* @return merged [OpenPGPCertificate]
*/
fun mergeCertificate( fun mergeCertificate(
originalCopy: OpenPGPCertificate, originalCopy: OpenPGPCertificate,
updatedCopy: OpenPGPCertificate updatedCopy: OpenPGPCertificate
@ -109,19 +183,25 @@ class PGPainless(
return OpenPGPCertificate.join(originalCopy, updatedCopy) return OpenPGPCertificate.join(originalCopy, updatedCopy)
} }
/** Generate an encrypted and/or signed OpenPGP message. */ /**
* Generate an encrypted and/or signed OpenPGP message.
*
* @return [EncryptionBuilder] api
*/
fun generateMessage(): EncryptionBuilder = EncryptionBuilder(this) fun generateMessage(): EncryptionBuilder = EncryptionBuilder(this)
/** /**
* Process an OpenPGP message. This method attempts decryption of encrypted messages and * Process an OpenPGP message. This method attempts decryption of encrypted messages and
* performs signature verification. * performs signature verification.
*
* @return [DecryptionBuilder] api
*/ */
fun processMessage(): DecryptionBuilder = DecryptionBuilder(this) fun processMessage(): DecryptionBuilder = DecryptionBuilder(this)
/** /**
* Create certification signatures on third-party [OpenPGPCertificates][OpenPGPCertificate]. * Create certification signatures on third-party [OpenPGPCertificates][OpenPGPCertificate].
* *
* @return builder * @return [CertifyCertificate] api
*/ */
fun generateCertification(): CertifyCertificate = CertifyCertificate(this) fun generateCertification(): CertifyCertificate = CertifyCertificate(this)
@ -289,7 +369,7 @@ class PGPainless(
* [PGPSecretKeyRing]. This method can be used to determine expiration dates, key flags and * [PGPSecretKeyRing]. This method can be used to determine expiration dates, key flags and
* other information about a key at a specific time. * other information about a key at a specific time.
* *
* @param keyRing key ring * @param key key ring
* @param referenceTime date of inspection * @param referenceTime date of inspection
* @return access object * @return access object
*/ */