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

Kotlin conversion: EncryptionBuilder + Interface

This commit is contained in:
Paul Schaub 2023-09-01 18:23:12 +02:00
parent bbd956dbb7
commit e9caa4af1f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 95 additions and 115 deletions

View file

@ -1,77 +0,0 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.encryption_signing;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPException;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.algorithm.negotiation.SymmetricKeyAlgorithmNegotiator;
import org.pgpainless.key.SubkeyIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EncryptionBuilder implements EncryptionBuilderInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(EncryptionBuilder.class);
private OutputStream outputStream;
@Override
public WithOptions onOutputStream(@Nonnull OutputStream outputStream) {
this.outputStream = outputStream;
return new WithOptionsImpl();
}
class WithOptionsImpl implements WithOptions {
@Override
public EncryptionStream withOptions(ProducerOptions options) throws PGPException, IOException {
if (options == null) {
throw new NullPointerException("ProducerOptions cannot be null.");
}
return new EncryptionStream(outputStream, options);
}
}
/**
* Negotiate the {@link SymmetricKeyAlgorithm} used for message encryption.
*
* @param encryptionOptions encryption options
* @return negotiated symmetric key algorithm
*/
public static SymmetricKeyAlgorithm negotiateSymmetricEncryptionAlgorithm(EncryptionOptions encryptionOptions) {
List<Set<SymmetricKeyAlgorithm>> preferences = new ArrayList<>();
for (SubkeyIdentifier key : encryptionOptions.getKeyViews().keySet()) {
preferences.add(encryptionOptions.getKeyViews().get(key).getPreferredSymmetricKeyAlgorithms());
}
SymmetricKeyAlgorithm algorithm = SymmetricKeyAlgorithmNegotiator
.byPopularity()
.negotiate(
PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy(),
encryptionOptions.getEncryptionAlgorithmOverride(),
preferences);
LOGGER.debug("Negotiation resulted in {} being the symmetric encryption algorithm of choice.", algorithm);
return algorithm;
}
public static CompressionAlgorithm negotiateCompressionAlgorithm(ProducerOptions producerOptions) {
CompressionAlgorithm compressionAlgorithmOverride = producerOptions.getCompressionAlgorithmOverride();
if (compressionAlgorithmOverride != null) {
return compressionAlgorithmOverride;
}
// TODO: Negotiation
return PGPainless.getPolicy().getCompressionAlgorithmPolicy().defaultCompressionAlgorithm();
}
}

View file

@ -1,38 +0,0 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.encryption_signing;
import java.io.IOException;
import java.io.OutputStream;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPException;
public interface EncryptionBuilderInterface {
/**
* Create a {@link EncryptionStream} on an {@link OutputStream} that contains the plain data that
* shall be encrypted and or signed.
*
* @param outputStream output stream of the plain data.
* @return api handle
*/
WithOptions onOutputStream(@Nonnull OutputStream outputStream);
interface WithOptions {
/**
* Create an {@link EncryptionStream} with the given options (recipients, signers, algorithms...).
*
* @param options options
* @return encryption stream
*
* @throws PGPException if something goes wrong during encryption stream preparation
* @throws IOException if something goes wrong during encryption stream preparation (writing headers)
*/
EncryptionStream withOptions(ProducerOptions options) throws PGPException, IOException;
}
}