1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-25 21:14:49 +02:00

KeyRingCertificateStore: Allow for multiple PGPPublicKeyRingCollections as input

This commit is contained in:
Paul Schaub 2023-07-07 15:49:39 +02:00
parent b1f33080e0
commit 34f706191d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -15,25 +15,33 @@ import pgp.certificate_store.exception.BadNameException
import java.io.InputStream import java.io.InputStream
/** /**
* Implementation of [PGPCertificateStore] which is based on a [PGPPublicKeyRingCollection]. * Implementation of [PGPCertificateStore] which is based on one or more [PGPPublicKeyRingCollection].
* During initialization, all items in the [PGPPublicKeyRingCollection] are converted into [Certificates][Certificate] * During initialization, all items in the [PGPPublicKeyRingCollection]s are converted into [Certificates][Certificate]
* and stored in a map keyed by their fingerprints. * and stored in a map keyed by their fingerprints.
*
* In case of fingerprint collisions across certificates from different collections, [Certificate] objects
* from a [PGPPublicKeyRingCollection] instance with a higher list index take precedence.
*
* [Certificates][Certificate] being inserted using [insertCertificate] or [insertCertificateBySpecialName] are also * [Certificates][Certificate] being inserted using [insertCertificate] or [insertCertificateBySpecialName] are also
* stored in that map, but are not being written into the [PGPPublicKeyRingCollection]. * stored in that map, but are not being written into the [PGPPublicKeyRingCollection].
*/ */
class KeyRingCertificateStore(baseKeyRing: PGPPublicKeyRingCollection) : PGPCertificateStore { class KeyRingCertificateStore(baseKeyRings: List<PGPPublicKeyRingCollection>) : PGPCertificateStore {
// Keep certificates inserted only in memory // Keep certificates inserted only in memory
private val certificates = mutableMapOf<String, Certificate>() private val certificates = mutableMapOf<String, Certificate>()
init { init {
for (publicKeyRing in baseKeyRing) { baseKeyRings.forEach { store ->
val fingerprint = OpenPgpFingerprint.of(publicKeyRing).toString() store.forEach {
val certificate = CertificateFactory.certificateFromPublicKeyRing(publicKeyRing, null) val fingerprint = OpenPgpFingerprint.of(it).toString()
certificates[fingerprint] = certificate val certificate = CertificateFactory.certificateFromPublicKeyRing(it, null)
certificates[fingerprint] = certificate
}
} }
} }
constructor(baseKeyRing: PGPPublicKeyRingCollection): this(listOf(baseKeyRing))
override fun getCertificate(identifier: String?): Certificate { override fun getCertificate(identifier: String?): Certificate {
if (identifier == null) { if (identifier == null) {
throw BadNameException("Identifier MUST NOT be null.") throw BadNameException("Identifier MUST NOT be null.")