1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-08 13:21:09 +01:00

Change return type of KeyRingBuilder.build() to OpenPGPKey

This commit is contained in:
Paul Schaub 2025-02-10 13:34:07 +01:00
parent 01fbf98beb
commit 337dbbbc0a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
74 changed files with 319 additions and 188 deletions

View file

@ -74,7 +74,7 @@ class PGPainless(
@JvmStatic
@JvmOverloads
fun buildKeyRing(version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4) =
KeyRingBuilder(version)
KeyRingBuilder(version, getInstance().implementation)
/**
* Read an existing OpenPGP key ring.

View file

@ -8,6 +8,7 @@ import java.io.IOException
import java.util.*
import org.bouncycastle.openpgp.*
import org.bouncycastle.openpgp.api.OpenPGPImplementation
import org.bouncycastle.openpgp.api.OpenPGPKey
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder
@ -25,8 +26,10 @@ import org.pgpainless.signature.subpackets.SignatureSubpackets
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper
import org.pgpainless.util.Passphrase
class KeyRingBuilder(private val version: OpenPGPKeyVersion) :
KeyRingBuilderInterface<KeyRingBuilder> {
class KeyRingBuilder(
private val version: OpenPGPKeyVersion,
private val implementation: OpenPGPImplementation
) : KeyRingBuilderInterface<KeyRingBuilder> {
private var primaryKeySpec: KeySpec? = null
private val subKeySpecs = mutableListOf<KeySpec>()
@ -80,7 +83,7 @@ class KeyRingBuilder(private val version: OpenPGPKeyVersion) :
private fun keyIsCertificationCapable(keySpec: KeySpec) = keySpec.keyType.canCertify
override fun build(): PGPSecretKeyRing {
override fun build(): OpenPGPKey {
val keyFingerprintCalculator = ImplementationFactory.getInstance().v4FingerprintCalculator
val secretKeyEncryptor = buildSecretKeyEncryptor(keyFingerprintCalculator)
val secretKeyDecryptor = buildSecretKeyDecryptor()
@ -168,7 +171,8 @@ class KeyRingBuilder(private val version: OpenPGPKeyVersion) :
while (secretKeys.hasNext()) {
secretKeyList.add(secretKeys.next())
}
return PGPSecretKeyRing(secretKeyList)
val pgpSecretKeyRing = PGPSecretKeyRing(secretKeyList)
return OpenPGPKey(pgpSecretKeyRing, implementation)
}
private fun addSubKeys(primaryKey: PGPKeyPair, ringGenerator: PGPKeyRingGenerator) {

View file

@ -8,7 +8,7 @@ import java.security.InvalidAlgorithmParameterException
import java.security.NoSuchAlgorithmException
import java.util.*
import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPSecretKeyRing
import org.bouncycastle.openpgp.api.OpenPGPKey
import org.pgpainless.util.Passphrase
interface KeyRingBuilderInterface<B : KeyRingBuilderInterface<B>> {
@ -33,5 +33,5 @@ interface KeyRingBuilderInterface<B : KeyRingBuilderInterface<B>> {
NoSuchAlgorithmException::class,
PGPException::class,
InvalidAlgorithmParameterException::class)
fun build(): PGPSecretKeyRing
fun build(): OpenPGPKey
}

View file

@ -4,7 +4,7 @@
package org.pgpainless.key.generation
import org.bouncycastle.openpgp.PGPSecretKeyRing
import org.bouncycastle.openpgp.api.OpenPGPKey
import org.pgpainless.PGPainless.Companion.buildKeyRing
import org.pgpainless.algorithm.KeyFlag
import org.pgpainless.algorithm.OpenPGPKeyVersion
@ -24,14 +24,14 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
* @param userId userId or null
* @param length length of the RSA keys
* @param passphrase passphrase to encrypt the key with. Can be empty for an unencrytped key.
* @return key
* @return [OpenPGPKey]
*/
@JvmOverloads
fun rsaKeyRing(
userId: CharSequence?,
length: RsaLength,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): PGPSecretKeyRing =
): OpenPGPKey =
buildKeyRing(version)
.apply {
setPrimaryKey(getBuilder(KeyType.RSA(length), KeyFlag.CERTIFY_OTHER))
@ -53,9 +53,9 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
* @param length length of the RSA keys
* @param password passphrase to encrypt the key with. Can be null or blank for unencrypted
* keys.
* @return key
* @return [OpenPGPKey]
*/
fun rsaKeyRing(userId: CharSequence?, length: RsaLength, password: String?): PGPSecretKeyRing =
fun rsaKeyRing(userId: CharSequence?, length: RsaLength, password: String?): OpenPGPKey =
password.let {
if (it.isNullOrBlank()) {
rsaKeyRing(userId, length, Passphrase.emptyPassphrase())
@ -70,15 +70,15 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
*
* @param userId user id.
* @param length length in bits.
* @param password Password of the key. Can be empty for unencrypted keys.
* @return [PGPSecretKeyRing] containing the KeyPair.
* @param passphrase Password of the key. Can be empty for unencrypted keys.
* @return [OpenPGPKey]
*/
@JvmOverloads
fun simpleRsaKeyRing(
userId: CharSequence?,
length: RsaLength,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): PGPSecretKeyRing =
): OpenPGPKey =
buildKeyRing(version)
.apply {
setPrimaryKey(
@ -101,7 +101,7 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
* @param userId user id.
* @param length length in bits.
* @param password Password of the key. Can be null or blank for unencrypted keys.
* @return [PGPSecretKeyRing] containing the KeyPair.
* @return [OpenPGPKey]
*/
fun simpleRsaKeyRing(userId: CharSequence?, length: RsaLength, password: String?) =
password.let {
@ -119,13 +119,13 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
*
* @param userId user-id
* @param passphrase Password of the private key. Can be empty for an unencrypted key.
* @return [PGPSecretKeyRing] containing the key pairs.
* @return [OpenPGPKey]
*/
@JvmOverloads
fun simpleEcKeyRing(
userId: CharSequence?,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): PGPSecretKeyRing {
): OpenPGPKey {
val signingKeyType =
if (version == OpenPGPKeyVersion.v6) KeyType.Ed25519()
else KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519)
@ -151,10 +151,10 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
* used for encryption and decryption of messages.
*
* @param userId user-id
* @param passphrase Password of the private key. Can be null or blank for an unencrypted key.
* @return [PGPSecretKeyRing] containing the key pairs.
* @param password Password of the private key. Can be null or blank for an unencrypted key.
* @return [OpenPGPKey]
*/
fun simpleEcKeyRing(userId: CharSequence?, password: String?): PGPSecretKeyRing =
fun simpleEcKeyRing(userId: CharSequence?, password: String?): OpenPGPKey =
password.let {
if (it.isNullOrBlank()) {
simpleEcKeyRing(userId, Passphrase.emptyPassphrase())
@ -169,13 +169,13 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
*
* @param userId primary user id
* @param passphrase passphrase for the private key. Can be empty for an unencrypted key.
* @return key ring
* @return [OpenPGPKey]
*/
@JvmOverloads
fun modernKeyRing(
userId: CharSequence?,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): PGPSecretKeyRing {
): OpenPGPKey {
val signingKeyType =
if (version == OpenPGPKeyVersion.v6) KeyType.Ed25519()
else KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519)
@ -202,9 +202,9 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
*
* @param userId primary user id
* @param password passphrase for the private key. Can be null or blank for an unencrypted key.
* @return key ring
* @return [OpenPGPKey]
*/
fun modernKeyRing(userId: CharSequence?, password: String?): PGPSecretKeyRing =
fun modernKeyRing(userId: CharSequence?, password: String?): OpenPGPKey =
password.let {
if (it.isNullOrBlank()) {
modernKeyRing(userId, Passphrase.emptyPassphrase())