WIP: Add method to add image attribute

This commit is contained in:
Paul Schaub 2024-02-02 16:01:47 +01:00
parent 44a3096467
commit a40c6ac755
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 33 additions and 0 deletions

View File

@ -4,11 +4,14 @@
package org.pgpainless.key.generation
import org.bouncycastle.bcpg.attr.ImageAttribute
import java.util.*
import org.bouncycastle.openpgp.PGPSecretKey
import org.bouncycastle.openpgp.PGPSecretKeyRing
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVectorGenerator
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor
import org.bouncycastle.util.io.Streams
import org.pgpainless.algorithm.AlgorithmSuite
import org.pgpainless.algorithm.KeyFlag
import org.pgpainless.implementation.ImplementationFactory
@ -16,6 +19,8 @@ import org.pgpainless.key.generation.type.KeyType
import org.pgpainless.key.protection.SecretKeyRingProtector
import org.pgpainless.policy.Policy
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets
import java.io.File
import java.io.IOException
/**
* OpenPGP key builder. This implementation supersedes the old [KeyRingBuilder].
@ -140,6 +145,27 @@ open class GenerateOpenPgpKey(
attribute, subpacketsCallback = preferencesCallback.then(subpacketsCallback))
}
/**
* Add the contents of a JPEG file as image attribute to the key.
*
* @param jpegFile file containing a JPEG image
* @param subpacketsCallback callback to modify the user-attribute binding signature subpackets.
* @return this
*/
@Throws(IOException::class)
fun addJpegImage(
jpegFile: File,
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply {
jpegFile.inputStream()
.let { Streams.readAll(it) }
.let {
PGPUserAttributeSubpacketVectorGenerator().apply {
setImageAttribute(ImageAttribute.JPEG, it)
}.generate()
}.let { addUserAttribute(it, subpacketsCallback) }
}
/**
* Add a subkey to the key. The subpackets of the binding signature will be populated with
* issuer information, the passed in [bindingTime] as signature creation time and given

View File

@ -71,4 +71,11 @@ class GenerateOpenPgpKeyTest {
v4Builder.addSigningSubkey(KeyType.RSA(RsaLength._2048)) // too weak
}
}
@Test
fun testKeyGenerationWithJPEGAttribute() {
GenerateOpenPgpKey(Policy.getInstance())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
.addJpegImage()
}
}