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

Port more extension functions

This commit is contained in:
Paul Schaub 2025-03-20 19:13:09 +01:00
parent b828e5477c
commit 665db5ceb6
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -4,7 +4,7 @@
package org.pgpainless.bouncycastle.extensions
import openpgp.openPgpKeyId
import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPOnePassSignature
import org.bouncycastle.openpgp.PGPPublicKey
@ -17,8 +17,17 @@ import org.pgpainless.key.SubkeyIdentifier
/** Return true, if this [PGPKeyRing] contains the subkey identified by the [SubkeyIdentifier]. */
fun PGPKeyRing.matches(subkeyIdentifier: SubkeyIdentifier): Boolean =
this.publicKey.keyID == subkeyIdentifier.primaryKeyId &&
this.getPublicKey(subkeyIdentifier.subkeyId) != null
this.publicKey.keyIdentifier.matches(subkeyIdentifier.certificateIdentifier) &&
this.getPublicKey(subkeyIdentifier.componentKeyIdentifier) != null
/**
* Return true, if the [PGPKeyRing] contains a public key with the given [keyIdentifier].
*
* @param keyIdentifier KeyIdentifier
* @return true if key with the given key-ID is present, false otherwise
*/
fun PGPKeyRing.hasPublicKey(keyIdentifier: KeyIdentifier): Boolean =
this.getPublicKey(keyIdentifier) != null
/**
* Return true, if the [PGPKeyRing] contains a public key with the given key-ID.
@ -26,7 +35,8 @@ fun PGPKeyRing.matches(subkeyIdentifier: SubkeyIdentifier): Boolean =
* @param keyId keyId
* @return true if key with the given key-ID is present, false otherwise
*/
fun PGPKeyRing.hasPublicKey(keyId: Long): Boolean = this.getPublicKey(keyId) != null
@Deprecated("Pass in a KeyIdentifier instead.")
fun PGPKeyRing.hasPublicKey(keyId: Long): Boolean = hasPublicKey(KeyIdentifier(keyId))
/**
* Return true, if the [PGPKeyRing] contains a public key with the given fingerprint.
@ -35,7 +45,7 @@ fun PGPKeyRing.hasPublicKey(keyId: Long): Boolean = this.getPublicKey(keyId) !=
* @return true if key with the given fingerprint is present, false otherwise
*/
fun PGPKeyRing.hasPublicKey(fingerprint: OpenPgpFingerprint): Boolean =
this.getPublicKey(fingerprint) != null
hasPublicKey(fingerprint.keyIdentifier)
/**
* Return the [PGPPublicKey] with the given [OpenPgpFingerprint] or null, if no such key is present.
@ -44,17 +54,17 @@ fun PGPKeyRing.hasPublicKey(fingerprint: OpenPgpFingerprint): Boolean =
* @return public key
*/
fun PGPKeyRing.getPublicKey(fingerprint: OpenPgpFingerprint): PGPPublicKey? =
this.getPublicKey(fingerprint.bytes)
this.getPublicKey(fingerprint.keyIdentifier)
fun PGPKeyRing.requirePublicKey(keyId: Long): PGPPublicKey =
getPublicKey(keyId)
?: throw NoSuchElementException(
"OpenPGP key does not contain key with id ${keyId.openPgpKeyId()}.")
fun PGPKeyRing.requirePublicKey(keyIdentifier: KeyIdentifier): PGPPublicKey =
getPublicKey(keyIdentifier)
?: throw NoSuchElementException("OpenPGP key does not contain key with id $keyIdentifier.")
@Deprecated("Pass in a KeyIdentifier instead.")
fun PGPKeyRing.requirePublicKey(keyId: Long): PGPPublicKey = requirePublicKey(KeyIdentifier(keyId))
fun PGPKeyRing.requirePublicKey(fingerprint: OpenPgpFingerprint): PGPPublicKey =
getPublicKey(fingerprint)
?: throw NoSuchElementException(
"OpenPGP key does not contain key with fingerprint $fingerprint.")
requirePublicKey(fingerprint.keyIdentifier)
/**
* Return the [PGPPublicKey] that matches the [OpenPgpFingerprint] of the given [PGPSignature]. If
@ -62,11 +72,12 @@ fun PGPKeyRing.requirePublicKey(fingerprint: OpenPgpFingerprint): PGPPublicKey =
* subpacket to identify the [PGPPublicKey] via its key-ID.
*/
fun PGPKeyRing.getPublicKeyFor(signature: PGPSignature): PGPPublicKey? =
signature.fingerprint?.let { this.getPublicKey(it) } ?: this.getPublicKey(signature.keyID)
signature.fingerprint?.let { this.getPublicKey(it.keyIdentifier) }
?: this.getPublicKey(signature.keyID)
/** Return the [PGPPublicKey] that matches the key-ID of the given [PGPOnePassSignature] packet. */
fun PGPKeyRing.getPublicKeyFor(onePassSignature: PGPOnePassSignature): PGPPublicKey? =
this.getPublicKey(onePassSignature.keyID)
this.getPublicKey(onePassSignature.keyIdentifier)
/** Return the [OpenPgpFingerprint] of this OpenPGP key. */
val PGPKeyRing.openPgpFingerprint: OpenPgpFingerprint