mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 10:19:39 +02:00
Migrate some extension functions
This commit is contained in:
parent
053f6cf362
commit
b828e5477c
3 changed files with 53 additions and 17 deletions
|
@ -5,8 +5,12 @@
|
||||||
package org.pgpainless.bouncycastle.extensions
|
package org.pgpainless.bouncycastle.extensions
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData
|
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData
|
||||||
|
import org.bouncycastle.openpgp.PGPSignature
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPKey
|
import org.bouncycastle.openpgp.api.OpenPGPKey
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPSecretKey
|
import org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPSecretKey
|
||||||
|
|
||||||
fun OpenPGPKey.getSecretKeyFor(pkesk: PGPPublicKeyEncryptedData): OpenPGPSecretKey? =
|
fun OpenPGPKey.getSecretKeyFor(pkesk: PGPPublicKeyEncryptedData): OpenPGPSecretKey? =
|
||||||
this.getSecretKey(pkesk.keyIdentifier)
|
this.getSecretKey(pkesk.keyIdentifier)
|
||||||
|
|
||||||
|
fun OpenPGPKey.getSecretKeyFor(signature: PGPSignature): OpenPGPSecretKey? =
|
||||||
|
this.getSecretKey(signature.fingerprint!!.keyIdentifier)
|
||||||
|
|
|
@ -4,8 +4,13 @@
|
||||||
|
|
||||||
package org.pgpainless.bouncycastle.extensions
|
package org.pgpainless.bouncycastle.extensions
|
||||||
|
|
||||||
import openpgp.openPgpKeyId
|
import org.bouncycastle.bcpg.KeyIdentifier
|
||||||
import org.bouncycastle.openpgp.*
|
import org.bouncycastle.openpgp.PGPOnePassSignature
|
||||||
|
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData
|
||||||
|
import org.bouncycastle.openpgp.PGPPublicKeyRing
|
||||||
|
import org.bouncycastle.openpgp.PGPSecretKey
|
||||||
|
import org.bouncycastle.openpgp.PGPSecretKeyRing
|
||||||
|
import org.bouncycastle.openpgp.PGPSignature
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPImplementation
|
import org.bouncycastle.openpgp.api.OpenPGPImplementation
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPKey
|
import org.bouncycastle.openpgp.api.OpenPGPKey
|
||||||
import org.pgpainless.PGPainless
|
import org.pgpainless.PGPainless
|
||||||
|
@ -21,7 +26,17 @@ val PGPSecretKeyRing.certificate: PGPPublicKeyRing
|
||||||
* @param keyId keyId of the secret key
|
* @param keyId keyId of the secret key
|
||||||
* @return true, if the [PGPSecretKeyRing] has a matching [PGPSecretKey], false otherwise
|
* @return true, if the [PGPSecretKeyRing] has a matching [PGPSecretKey], false otherwise
|
||||||
*/
|
*/
|
||||||
fun PGPSecretKeyRing.hasSecretKey(keyId: Long): Boolean = this.getSecretKey(keyId) != null
|
@Deprecated("Pass in a KeyIdentifier instead.")
|
||||||
|
fun PGPSecretKeyRing.hasSecretKey(keyId: Long): Boolean = hasSecretKey(KeyIdentifier(keyId))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true, if the [PGPSecretKeyRing] contains a [PGPSecretKey] with the given [keyIdentifier].
|
||||||
|
*
|
||||||
|
* @param keyIdentifier identifier of the secret key
|
||||||
|
* @return true, if the [PGPSecretKeyRing] has a matching [PGPSecretKey], false otherwise
|
||||||
|
*/
|
||||||
|
fun PGPSecretKeyRing.hasSecretKey(keyIdentifier: KeyIdentifier): Boolean =
|
||||||
|
this.getSecretKey(keyIdentifier) != null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return true, if the [PGPSecretKeyRing] contains a [PGPSecretKey] with the given fingerprint.
|
* Return true, if the [PGPSecretKeyRing] contains a [PGPSecretKey] with the given fingerprint.
|
||||||
|
@ -30,7 +45,7 @@ fun PGPSecretKeyRing.hasSecretKey(keyId: Long): Boolean = this.getSecretKey(keyI
|
||||||
* @return true, if the [PGPSecretKeyRing] has a matching [PGPSecretKey], false otherwise
|
* @return true, if the [PGPSecretKeyRing] has a matching [PGPSecretKey], false otherwise
|
||||||
*/
|
*/
|
||||||
fun PGPSecretKeyRing.hasSecretKey(fingerprint: OpenPgpFingerprint): Boolean =
|
fun PGPSecretKeyRing.hasSecretKey(fingerprint: OpenPgpFingerprint): Boolean =
|
||||||
this.getSecretKey(fingerprint) != null
|
hasSecretKey(fingerprint.keyIdentifier)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the [PGPSecretKey] with the given [OpenPgpFingerprint].
|
* Return the [PGPSecretKey] with the given [OpenPgpFingerprint].
|
||||||
|
@ -39,7 +54,7 @@ fun PGPSecretKeyRing.hasSecretKey(fingerprint: OpenPgpFingerprint): Boolean =
|
||||||
* @return the secret key or null
|
* @return the secret key or null
|
||||||
*/
|
*/
|
||||||
fun PGPSecretKeyRing.getSecretKey(fingerprint: OpenPgpFingerprint): PGPSecretKey? =
|
fun PGPSecretKeyRing.getSecretKey(fingerprint: OpenPgpFingerprint): PGPSecretKey? =
|
||||||
this.getSecretKey(fingerprint.bytes)
|
this.getSecretKey(fingerprint.keyIdentifier)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the [PGPSecretKey] with the given key-ID.
|
* Return the [PGPSecretKey] with the given key-ID.
|
||||||
|
@ -47,10 +62,20 @@ fun PGPSecretKeyRing.getSecretKey(fingerprint: OpenPgpFingerprint): PGPSecretKey
|
||||||
* @throws NoSuchElementException if the OpenPGP key doesn't contain a secret key with the given
|
* @throws NoSuchElementException if the OpenPGP key doesn't contain a secret key with the given
|
||||||
* key-ID
|
* key-ID
|
||||||
*/
|
*/
|
||||||
|
@Deprecated("Pass in a KeyIdentifier instead.")
|
||||||
fun PGPSecretKeyRing.requireSecretKey(keyId: Long): PGPSecretKey =
|
fun PGPSecretKeyRing.requireSecretKey(keyId: Long): PGPSecretKey =
|
||||||
getSecretKey(keyId)
|
requireSecretKey(KeyIdentifier(keyId))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the [PGPSecretKey] with the given [keyIdentifier].
|
||||||
|
*
|
||||||
|
* @throws NoSuchElementException if the OpenPGP key doesn't contain a secret key with the given
|
||||||
|
* keyIdentifier
|
||||||
|
*/
|
||||||
|
fun PGPSecretKeyRing.requireSecretKey(keyIdentifier: KeyIdentifier): PGPSecretKey =
|
||||||
|
getSecretKey(keyIdentifier)
|
||||||
?: throw NoSuchElementException(
|
?: throw NoSuchElementException(
|
||||||
"OpenPGP key does not contain key with id ${keyId.openPgpKeyId()}.")
|
"OpenPGP key does not contain key with id ${keyIdentifier}.")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the [PGPSecretKey] with the given fingerprint.
|
* Return the [PGPSecretKey] with the given fingerprint.
|
||||||
|
@ -59,9 +84,7 @@ fun PGPSecretKeyRing.requireSecretKey(keyId: Long): PGPSecretKey =
|
||||||
* fingerprint
|
* fingerprint
|
||||||
*/
|
*/
|
||||||
fun PGPSecretKeyRing.requireSecretKey(fingerprint: OpenPgpFingerprint): PGPSecretKey =
|
fun PGPSecretKeyRing.requireSecretKey(fingerprint: OpenPgpFingerprint): PGPSecretKey =
|
||||||
getSecretKey(fingerprint)
|
requireSecretKey(fingerprint.keyIdentifier)
|
||||||
?: throw NoSuchElementException(
|
|
||||||
"OpenPGP key does not contain key with fingerprint $fingerprint.")
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the [PGPSecretKey] that matches the [OpenPgpFingerprint] of the given [PGPSignature]. If
|
* Return the [PGPSecretKey] that matches the [OpenPgpFingerprint] of the given [PGPSignature]. If
|
||||||
|
@ -73,7 +96,7 @@ fun PGPSecretKeyRing.getSecretKeyFor(signature: PGPSignature): PGPSecretKey? =
|
||||||
|
|
||||||
/** Return the [PGPSecretKey] that matches the key-ID of the given [PGPOnePassSignature] packet. */
|
/** Return the [PGPSecretKey] that matches the key-ID of the given [PGPOnePassSignature] packet. */
|
||||||
fun PGPSecretKeyRing.getSecretKeyFor(onePassSignature: PGPOnePassSignature): PGPSecretKey? =
|
fun PGPSecretKeyRing.getSecretKeyFor(onePassSignature: PGPOnePassSignature): PGPSecretKey? =
|
||||||
this.getSecretKey(onePassSignature.keyID)
|
this.getSecretKey(onePassSignature.keyIdentifier)
|
||||||
|
|
||||||
fun PGPSecretKeyRing.getSecretKeyFor(pkesk: PGPPublicKeyEncryptedData): PGPSecretKey? =
|
fun PGPSecretKeyRing.getSecretKeyFor(pkesk: PGPPublicKeyEncryptedData): PGPSecretKey? =
|
||||||
this.getSecretKey(pkesk.keyIdentifier)
|
this.getSecretKey(pkesk.keyIdentifier)
|
||||||
|
|
|
@ -22,28 +22,37 @@ class PGPSecretKeyRingExtensionsTest {
|
||||||
@Test
|
@Test
|
||||||
fun testHasPgpSecretKeyRing() {
|
fun testHasPgpSecretKeyRing() {
|
||||||
val key = TestKeys.getEmilSecretKeyRing()
|
val key = TestKeys.getEmilSecretKeyRing()
|
||||||
assertTrue(key.hasSecretKey(TestKeys.EMIL_KEY_ID))
|
assertTrue(key.hasSecretKey(TestKeys.EMIL_FINGERPRINT.keyIdentifier))
|
||||||
|
assertTrue(key.hasSecretKey(TestKeys.EMIL_FINGERPRINT.keyId))
|
||||||
assertTrue(key.hasSecretKey(TestKeys.EMIL_FINGERPRINT))
|
assertTrue(key.hasSecretKey(TestKeys.EMIL_FINGERPRINT))
|
||||||
|
|
||||||
assertFalse(key.hasSecretKey(TestKeys.ROMEO_KEY_ID))
|
assertFalse(key.hasSecretKey(TestKeys.ROMEO_FINGERPRINT.keyIdentifier))
|
||||||
|
assertFalse(key.hasSecretKey(TestKeys.ROMEO_FINGERPRINT.keyId))
|
||||||
assertFalse(key.hasSecretKey(TestKeys.ROMEO_FINGERPRINT))
|
assertFalse(key.hasSecretKey(TestKeys.ROMEO_FINGERPRINT))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testRequireSecretKey() {
|
fun testRequireSecretKey() {
|
||||||
val key = TestKeys.getEmilSecretKeyRing()
|
val key = TestKeys.getEmilSecretKeyRing()
|
||||||
assertNotNull(key.requireSecretKey(TestKeys.EMIL_KEY_ID))
|
assertNotNull(key.requireSecretKey(TestKeys.EMIL_FINGERPRINT.keyIdentifier))
|
||||||
|
assertNotNull(key.requireSecretKey(TestKeys.EMIL_FINGERPRINT.keyId))
|
||||||
assertNotNull(key.requireSecretKey(TestKeys.EMIL_FINGERPRINT))
|
assertNotNull(key.requireSecretKey(TestKeys.EMIL_FINGERPRINT))
|
||||||
|
|
||||||
assertThrows<NoSuchElementException> { key.requireSecretKey(TestKeys.ROMEO_KEY_ID) }
|
assertThrows<NoSuchElementException> {
|
||||||
|
key.requireSecretKey(TestKeys.ROMEO_FINGERPRINT.keyIdentifier)
|
||||||
|
}
|
||||||
|
assertThrows<NoSuchElementException> {
|
||||||
|
key.requireSecretKey(TestKeys.ROMEO_FINGERPRINT.keyId)
|
||||||
|
}
|
||||||
assertThrows<NoSuchElementException> { key.requireSecretKey(TestKeys.ROMEO_FINGERPRINT) }
|
assertThrows<NoSuchElementException> { key.requireSecretKey(TestKeys.ROMEO_FINGERPRINT) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testGetSecretKeyForSignature() {
|
fun testGetSecretKeyForSignature() {
|
||||||
val key = TestKeys.getEmilSecretKeyRing()
|
val key = TestKeys.getEmilKey()
|
||||||
val signer =
|
val signer =
|
||||||
PGPainless.encryptAndOrSign()
|
PGPainless.getInstance()
|
||||||
|
.generateMessage()
|
||||||
.onOutputStream(ByteArrayOutputStream())
|
.onOutputStream(ByteArrayOutputStream())
|
||||||
.withOptions(
|
.withOptions(
|
||||||
ProducerOptions.sign(
|
ProducerOptions.sign(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue