mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
KeySpecBuilder: Expose API for overriding default AEAD algorithms and features
This commit is contained in:
parent
92630e40a4
commit
b84f464b49
2 changed files with 24 additions and 6 deletions
|
@ -12,8 +12,7 @@ import org.pgpainless.signature.subpackets.SelfSignatureSubpackets
|
||||||
import org.pgpainless.signature.subpackets.SignatureSubpackets
|
import org.pgpainless.signature.subpackets.SignatureSubpackets
|
||||||
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil
|
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil
|
||||||
|
|
||||||
class KeySpecBuilder
|
class KeySpecBuilder(
|
||||||
constructor(
|
|
||||||
private val type: KeyType,
|
private val type: KeyType,
|
||||||
private val keyFlags: List<KeyFlag>,
|
private val keyFlags: List<KeyFlag>,
|
||||||
) : KeySpecBuilderInterface {
|
) : KeySpecBuilderInterface {
|
||||||
|
@ -27,6 +26,7 @@ constructor(
|
||||||
private var preferredSymmetricAlgorithms: Set<SymmetricKeyAlgorithm>? =
|
private var preferredSymmetricAlgorithms: Set<SymmetricKeyAlgorithm>? =
|
||||||
algorithmSuite.symmetricKeyAlgorithms
|
algorithmSuite.symmetricKeyAlgorithms
|
||||||
private var preferredAEADAlgorithms: Set<AEADCipherMode>? = algorithmSuite.aeadAlgorithms
|
private var preferredAEADAlgorithms: Set<AEADCipherMode>? = algorithmSuite.aeadAlgorithms
|
||||||
|
private var features: Set<Feature>? = algorithmSuite.features
|
||||||
private var keyCreationDate: Date? = null
|
private var keyCreationDate: Date? = null
|
||||||
|
|
||||||
constructor(type: KeyType, vararg keyFlags: KeyFlag) : this(type, listOf(*keyFlags))
|
constructor(type: KeyType, vararg keyFlags: KeyFlag) : this(type, listOf(*keyFlags))
|
||||||
|
@ -37,11 +37,13 @@ constructor(
|
||||||
|
|
||||||
override fun overridePreferredCompressionAlgorithms(
|
override fun overridePreferredCompressionAlgorithms(
|
||||||
vararg algorithms: CompressionAlgorithm
|
vararg algorithms: CompressionAlgorithm
|
||||||
): KeySpecBuilder = apply { this.preferredCompressionAlgorithms = algorithms.toSet() }
|
): KeySpecBuilder = apply {
|
||||||
|
this.preferredCompressionAlgorithms = if (algorithms.isEmpty()) null else algorithms.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
override fun overridePreferredHashAlgorithms(vararg algorithms: HashAlgorithm): KeySpecBuilder =
|
override fun overridePreferredHashAlgorithms(vararg algorithms: HashAlgorithm): KeySpecBuilder =
|
||||||
apply {
|
apply {
|
||||||
this.preferredHashAlgorithms = algorithms.toSet()
|
this.preferredHashAlgorithms = if (algorithms.isEmpty()) null else algorithms.toSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun overridePreferredSymmetricKeyAlgorithms(
|
override fun overridePreferredSymmetricKeyAlgorithms(
|
||||||
|
@ -50,7 +52,17 @@ constructor(
|
||||||
require(!algorithms.contains(SymmetricKeyAlgorithm.NULL)) {
|
require(!algorithms.contains(SymmetricKeyAlgorithm.NULL)) {
|
||||||
"NULL (unencrypted) is an invalid symmetric key algorithm preference."
|
"NULL (unencrypted) is an invalid symmetric key algorithm preference."
|
||||||
}
|
}
|
||||||
this.preferredSymmetricAlgorithms = algorithms.toSet()
|
this.preferredSymmetricAlgorithms = if (algorithms.isEmpty()) null else algorithms.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun overridePreferredAEADAlgorithms(
|
||||||
|
vararg algorithms: AEADCipherMode
|
||||||
|
): KeySpecBuilder = apply {
|
||||||
|
this.preferredAEADAlgorithms = if (algorithms.isEmpty()) null else algorithms.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun overrideFeatures(vararg features: Feature): KeySpecBuilder = apply {
|
||||||
|
this.features = if (features.isEmpty()) null else features.toSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setKeyCreationDate(creationDate: Date): KeySpecBuilder = apply {
|
override fun setKeyCreationDate(creationDate: Date): KeySpecBuilder = apply {
|
||||||
|
@ -65,7 +77,7 @@ constructor(
|
||||||
preferredHashAlgorithms?.let { setPreferredHashAlgorithms(it) }
|
preferredHashAlgorithms?.let { setPreferredHashAlgorithms(it) }
|
||||||
preferredSymmetricAlgorithms?.let { setPreferredSymmetricKeyAlgorithms(it) }
|
preferredSymmetricAlgorithms?.let { setPreferredSymmetricKeyAlgorithms(it) }
|
||||||
preferredAEADAlgorithms?.let { setPreferredAEADCiphersuites(it) }
|
preferredAEADAlgorithms?.let { setPreferredAEADCiphersuites(it) }
|
||||||
setFeatures(Feature.MODIFICATION_DETECTION)
|
features?.let { setFeatures(*it.toTypedArray()) }
|
||||||
}
|
}
|
||||||
.let { KeySpec(type, hashedSubpackets as SignatureSubpackets, false, keyCreationDate) }
|
.let { KeySpec(type, hashedSubpackets as SignatureSubpackets, false, keyCreationDate) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@
|
||||||
package org.pgpainless.key.generation
|
package org.pgpainless.key.generation
|
||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import org.pgpainless.algorithm.AEADCipherMode
|
||||||
import org.pgpainless.algorithm.CompressionAlgorithm
|
import org.pgpainless.algorithm.CompressionAlgorithm
|
||||||
|
import org.pgpainless.algorithm.Feature
|
||||||
import org.pgpainless.algorithm.HashAlgorithm
|
import org.pgpainless.algorithm.HashAlgorithm
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||||
|
|
||||||
|
@ -21,6 +23,10 @@ interface KeySpecBuilderInterface {
|
||||||
vararg algorithms: SymmetricKeyAlgorithm
|
vararg algorithms: SymmetricKeyAlgorithm
|
||||||
): KeySpecBuilder
|
): KeySpecBuilder
|
||||||
|
|
||||||
|
fun overridePreferredAEADAlgorithms(vararg algorithms: AEADCipherMode): KeySpecBuilder
|
||||||
|
|
||||||
|
fun overrideFeatures(vararg features: Feature): KeySpecBuilder
|
||||||
|
|
||||||
fun setKeyCreationDate(creationDate: Date): KeySpecBuilder
|
fun setKeyCreationDate(creationDate: Date): KeySpecBuilder
|
||||||
|
|
||||||
fun build(): KeySpec
|
fun build(): KeySpec
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue