pgpainless/src/main/java/de/vanitasvitae/crypto/pgpainless/decryption_verification/PainlessResult.java

143 lines
4.6 KiB
Java
Raw Normal View History

2018-06-11 01:33:49 +02:00
package de.vanitasvitae.crypto.pgpainless.decryption_verification;
2018-06-02 21:21:35 +02:00
2018-06-06 18:46:41 +02:00
import java.util.Collections;
import java.util.HashSet;
2018-06-02 21:21:35 +02:00
import java.util.Set;
2018-06-06 18:46:41 +02:00
import de.vanitasvitae.crypto.pgpainless.algorithm.CompressionAlgorithm;
import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm;
2018-06-10 17:12:44 +02:00
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
2018-06-06 18:46:41 +02:00
2018-06-02 21:21:35 +02:00
public class PainlessResult {
2018-06-06 18:46:41 +02:00
private final Set<Long> recipientKeyIds;
private final Long decryptionKeyId;
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
private final CompressionAlgorithm compressionAlgorithm;
private final boolean integrityProtected;
private final Set<Long> signatureKeyIds;
private final Set<Long> verifiedSignatureKeyIds;
public PainlessResult(Set<Long> recipientKeyIds,
Long decryptionKeyId,
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
CompressionAlgorithm algorithm,
boolean integrityProtected,
Set<Long> signatureKeyIds,
Set<Long> verifiedSignatureKeyIds) {
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
this.decryptionKeyId = decryptionKeyId;
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
this.compressionAlgorithm = algorithm;
this.integrityProtected = integrityProtected;
this.signatureKeyIds = Collections.unmodifiableSet(signatureKeyIds);
this.verifiedSignatureKeyIds = Collections.unmodifiableSet(verifiedSignatureKeyIds);
}
public Set<Long> getRecipientKeyIds() {
return recipientKeyIds;
}
2018-06-10 17:12:44 +02:00
public boolean isEncrypted() {
return !getRecipientKeyIds().isEmpty();
}
2018-06-06 18:46:41 +02:00
public Long getDecryptionKeyId() {
return decryptionKeyId;
}
public SymmetricKeyAlgorithm getSymmetricKeyAlgorithm() {
return symmetricKeyAlgorithm;
}
public CompressionAlgorithm getCompressionAlgorithm() {
return compressionAlgorithm;
}
public boolean isIntegrityProtected() {
return integrityProtected;
}
2018-06-10 17:12:44 +02:00
public Set<Long> getAllSignatureKeyIds() {
2018-06-06 18:46:41 +02:00
return signatureKeyIds;
}
2018-06-10 17:12:44 +02:00
public boolean isSigned() {
return !signatureKeyIds.isEmpty();
}
2018-06-06 18:46:41 +02:00
public Set<Long> getVerifiedSignatureKeyIds() {
return verifiedSignatureKeyIds;
}
2018-06-10 17:12:44 +02:00
public boolean isVerified() {
return !verifiedSignatureKeyIds.isEmpty();
}
public boolean containsVerifiedSignatureFrom(PGPPublicKeyRing publicKeys) {
for (PGPPublicKey key : publicKeys) {
long id = key.getKeyID();
if (verifiedSignatureKeyIds.contains(id)) {
return true;
}
}
return false;
}
2018-06-11 01:33:49 +02:00
static Builder getBuilder() {
2018-06-06 18:46:41 +02:00
return new Builder();
}
2018-06-11 01:33:49 +02:00
static class Builder {
2018-06-06 18:46:41 +02:00
private final Set<Long> recipientKeyIds = new HashSet<>();
private Long decryptionKeyId;
private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.NULL;
private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED;
private boolean integrityProtected = false;
private final Set<Long> signatureKeyIds = new HashSet<>();
private final Set<Long> verifiedSignatureKeyIds = new HashSet<>();
public Builder addRecipientKeyId(long id) {
this.recipientKeyIds.add(id);
return this;
}
public Builder setDecryptionKeyId(long id) {
this.decryptionKeyId = id;
return this;
}
public Builder setCompressionAlgorithm(CompressionAlgorithm algorithm) {
this.compressionAlgorithm = algorithm;
return this;
}
public Builder addSignatureKeyId(long id) {
this.signatureKeyIds.add(id);
return this;
}
public Builder addVerifiedSignatureKeyId(long id) {
this.verifiedSignatureKeyIds.add(id);
return this;
}
public Builder setSymmetricKeyAlgorithm(SymmetricKeyAlgorithm symmetricKeyAlgorithm) {
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
return this;
}
public Builder setIntegrityProtected(boolean integrityProtected) {
this.integrityProtected = integrityProtected;
return this;
}
public PainlessResult build() {
return new PainlessResult(recipientKeyIds, decryptionKeyId, symmetricKeyAlgorithm, compressionAlgorithm, integrityProtected, signatureKeyIds, verifiedSignatureKeyIds);
}
}
2018-06-02 21:21:35 +02:00
}