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

Kotlin conversion: SecretKeyPassphraseProvider and subclasses

This commit also adds a workaround to build.gradle which enables proper Java interop for
Kotlin interfaces with default implementations
This commit is contained in:
Paul Schaub 2023-09-01 13:43:04 +02:00
parent 5cb6d6e41d
commit e3f51fbf56
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 84 additions and 131 deletions

View file

@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.protection.passphrase_provider
import org.pgpainless.util.Passphrase
/**
* Implementation of the [SecretKeyPassphraseProvider] that holds a map of key-IDs and respective [Passphrase].
* It will return the right passphrase depending on the key-id.
*
* Note: This provider might return null!
* TODO: Make this null-safe and throw an exception instead?
*/
class MapBasedPassphraseProvider(val map: Map<Long, Passphrase>) : SecretKeyPassphraseProvider {
override fun getPassphraseFor(keyId: Long): Passphrase? = map[keyId]
override fun hasPassphrase(keyId: Long): Boolean = map.containsKey(keyId)
}

View file

@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.protection.passphrase_provider
import org.bouncycastle.openpgp.PGPSecretKey
import org.pgpainless.util.Passphrase
/**
* Interface to allow the user to provide a [Passphrase] for an encrypted OpenPGP secret key.
*/
interface SecretKeyPassphraseProvider {
/**
* Return a passphrase for the given secret key.
* If no record is found, return null.
* Note: In case of an unprotected secret key, this method must may not return null, but a [Passphrase] with
* a content of null.
*
* @param secretKey secret key
* @return passphrase or null, if no passphrase record is found.
*/
fun getPassphraseFor(secretKey: PGPSecretKey): Passphrase? {
return getPassphraseFor(secretKey.keyID)
}
/**
* Return a passphrase for the given key. If no record has been found, return null.
* Note: In case of an unprotected secret key, this method must may not return null, but a [Passphrase] with
* a content of null.
*
* @param keyId if of the secret key
* @return passphrase or null, if no passphrase record has been found.
*/
fun getPassphraseFor(keyId: Long): Passphrase?
fun hasPassphrase(keyId: Long): Boolean
}

View file

@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.protection.passphrase_provider
import org.pgpainless.util.Passphrase
/**
* Implementation of the [SecretKeyPassphraseProvider] that holds a single [Passphrase].
*/
class SolitaryPassphraseProvider(val passphrase: Passphrase?) : SecretKeyPassphraseProvider {
override fun getPassphraseFor(keyId: Long): Passphrase? = passphrase
override fun hasPassphrase(keyId: Long): Boolean = true
}