pgpainless/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java

83 lines
3.1 KiB
Java
Raw Normal View History

/*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.encryption_signing;
2018-06-05 01:30:58 +02:00
import java.io.IOException;
2018-06-04 19:45:18 +02:00
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
2021-05-25 13:52:45 +02:00
import java.util.Set;
2020-08-24 16:26:29 +02:00
import javax.annotation.Nonnull;
2018-06-05 01:30:58 +02:00
import org.bouncycastle.openpgp.PGPException;
2021-04-26 13:38:12 +02:00
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
2021-05-25 13:52:45 +02:00
import org.pgpainless.algorithm.negotiation.SymmetricKeyAlgorithmNegotiator;
import org.pgpainless.key.SubkeyIdentifier;
2018-06-04 19:45:18 +02:00
public class EncryptionBuilder implements EncryptionBuilderInterface {
private OutputStream outputStream;
@Override
public WithOptions onOutputStream(@Nonnull OutputStream outputStream) {
2018-06-04 19:45:18 +02:00
this.outputStream = outputStream;
2021-06-29 16:43:37 +02:00
return new WithOptionsImpl();
2018-06-04 19:45:18 +02:00
}
2021-06-29 16:43:37 +02:00
class WithOptionsImpl implements WithOptions {
2018-06-07 18:12:13 +02:00
@Override
2021-05-06 00:04:03 +02:00
public EncryptionStream withOptions(ProducerOptions options) throws PGPException, IOException {
if (options == null) {
throw new NullPointerException("ProducerOptions cannot be null.");
2018-06-07 18:12:13 +02:00
}
return new EncryptionStream(outputStream, options);
2018-06-04 19:45:18 +02:00
}
}
2018-06-11 01:33:49 +02:00
2021-05-06 00:04:03 +02:00
/**
* Negotiate the {@link SymmetricKeyAlgorithm} used for message encryption.
*
* @param encryptionOptions encryption options
* @return negotiated symmetric key algorithm
*/
public static SymmetricKeyAlgorithm negotiateSymmetricEncryptionAlgorithm(EncryptionOptions encryptionOptions) {
2021-05-25 13:52:45 +02:00
List<Set<SymmetricKeyAlgorithm>> preferences = new ArrayList<>();
for (SubkeyIdentifier key : encryptionOptions.getKeyViews().keySet()) {
2021-05-25 13:52:45 +02:00
preferences.add(encryptionOptions.getKeyViews().get(key).getPreferredSymmetricKeyAlgorithms());
}
2021-05-06 00:04:03 +02:00
2021-05-25 13:52:45 +02:00
return SymmetricKeyAlgorithmNegotiator
.byPopularity()
.negotiate(
PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy(),
encryptionOptions.getEncryptionAlgorithmOverride(),
preferences);
}
2021-05-06 00:04:03 +02:00
public static CompressionAlgorithm negotiateCompressionAlgorithm(ProducerOptions producerOptions) {
CompressionAlgorithm compressionAlgorithmOverride = producerOptions.getCompressionAlgorithmOverride();
if (compressionAlgorithmOverride != null) {
return compressionAlgorithmOverride;
}
2021-05-06 00:04:03 +02:00
// TODO: Negotiation
return PGPainless.getPolicy().getCompressionAlgorithmPolicy().defaultCompressionAlgorithm();
2018-06-11 01:33:49 +02:00
}
}