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

Introduce PGPainless.toKeyOrCertificate(PGPKeyRing) and constrain argument type of PGPainless.toCertificate(PGPPublicKeyRing)

This commit is contained in:
Paul Schaub 2025-03-24 12:57:46 +01:00
parent 221d329254
commit bab448eb6d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 20 additions and 14 deletions

View file

@ -91,8 +91,17 @@ class PGPainless(
fun toKey(secretKeyRing: PGPSecretKeyRing): OpenPGPKey = fun toKey(secretKeyRing: PGPSecretKeyRing): OpenPGPKey =
OpenPGPKey(secretKeyRing, implementation) OpenPGPKey(secretKeyRing, implementation)
fun toCertificate(keyOrCertificate: PGPKeyRing): OpenPGPCertificate = fun toCertificate(certificate: PGPPublicKeyRing): OpenPGPCertificate =
OpenPGPCertificate(keyOrCertificate, implementation) OpenPGPCertificate(certificate, implementation)
fun toKeyOrCertificate(keyOrCertificate: PGPKeyRing): OpenPGPCertificate =
when (keyOrCertificate) {
is PGPSecretKeyRing -> toKey(keyOrCertificate)
is PGPPublicKeyRing -> toCertificate(keyOrCertificate)
else ->
throw IllegalArgumentException(
"Unexpected PGPKeyRing subclass: ${keyOrCertificate.javaClass.name}")
}
fun mergeCertificate( fun mergeCertificate(
originalCopy: OpenPGPCertificate, originalCopy: OpenPGPCertificate,
@ -120,8 +129,8 @@ class PGPainless(
instance ?: synchronized(this) { instance ?: PGPainless().also { instance = it } } instance ?: synchronized(this) { instance ?: PGPainless().also { instance = it } }
@JvmStatic @JvmStatic
fun setInstance(pgpainless: PGPainless) { fun setInstance(api: PGPainless) {
instance = pgpainless instance = api
} }
/** /**
@ -145,10 +154,8 @@ class PGPainless(
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
@Deprecated("Call buildKey() on an instance of PGPainless instead.") @Deprecated("Call buildKey() on an instance of PGPainless instead.")
fun buildKeyRing( fun buildKeyRing(version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4): KeyRingBuilder =
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4, getInstance().buildKey(version)
api: PGPainless = getInstance()
): KeyRingBuilder = KeyRingBuilder(version, api)
/** /**
* Read an existing OpenPGP key ring. * Read an existing OpenPGP key ring.
@ -262,9 +269,8 @@ class PGPainless(
@JvmOverloads @JvmOverloads
fun modifyKeyRing( fun modifyKeyRing(
secretKey: PGPSecretKeyRing, secretKey: PGPSecretKeyRing,
referenceTime: Date = Date(), referenceTime: Date = Date()
api: PGPainless = getInstance() ): SecretKeyRingEditor = getInstance().modify(getInstance().toKey(secretKey), referenceTime)
): SecretKeyRingEditor = SecretKeyRingEditor(secretKey, api, referenceTime)
/** /**
* Quickly access information about a [org.bouncycastle.openpgp.PGPPublicKeyRing] / * Quickly access information about a [org.bouncycastle.openpgp.PGPPublicKeyRing] /
@ -281,7 +287,7 @@ class PGPainless(
"Use inspect(key) on an instance of PGPainless instead.", "Use inspect(key) on an instance of PGPainless instead.",
replaceWith = ReplaceWith("inspect(key)")) replaceWith = ReplaceWith("inspect(key)"))
fun inspectKeyRing(key: PGPKeyRing, referenceTime: Date = Date()): KeyRingInfo = fun inspectKeyRing(key: PGPKeyRing, referenceTime: Date = Date()): KeyRingInfo =
KeyRingInfo(key, referenceTime) getInstance().inspect(getInstance().toKeyOrCertificate(key), referenceTime)
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
@ -289,7 +295,7 @@ class PGPainless(
"Use inspect(key) on an instance of PGPainless instead.", "Use inspect(key) on an instance of PGPainless instead.",
replaceWith = ReplaceWith("inspect(key)")) replaceWith = ReplaceWith("inspect(key)"))
fun inspectKeyRing(key: OpenPGPCertificate, referenceTime: Date = Date()): KeyRingInfo = fun inspectKeyRing(key: OpenPGPCertificate, referenceTime: Date = Date()): KeyRingInfo =
KeyRingInfo(key, getInstance(), referenceTime) getInstance().inspect(key, referenceTime)
/** /**
* Access, and make changes to PGPainless policy on acceptable/default algorithms etc. * Access, and make changes to PGPainless policy on acceptable/default algorithms etc.

View file

@ -83,7 +83,7 @@ abstract class SelectUserId : Predicate<String>, (String) -> Boolean {
@JvmStatic @JvmStatic
fun validUserId(keyRing: PGPKeyRing) = fun validUserId(keyRing: PGPKeyRing) =
validUserId(PGPainless.getInstance().toCertificate(keyRing)) validUserId(PGPainless.getInstance().toKeyOrCertificate(keyRing))
@JvmStatic @JvmStatic
fun and(vararg filters: SelectUserId) = fun and(vararg filters: SelectUserId) =