001/*
002 * Copyright 2018 Paul Schaub.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.pgpainless;
017
018import javax.annotation.Nonnull;
019import java.io.IOException;
020
021import org.bouncycastle.openpgp.PGPException;
022import org.pgpainless.algorithm.CompressionAlgorithm;
023import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
024import org.pgpainless.decryption_verification.DecryptionBuilder;
025import org.pgpainless.decryption_verification.DecryptionStream;
026import org.pgpainless.encryption_signing.EncryptionBuilder;
027import org.pgpainless.encryption_signing.EncryptionStream;
028import org.pgpainless.key.generation.KeyRingBuilder;
029import org.pgpainless.key.parsing.KeyRingReader;
030import org.pgpainless.symmetric_encryption.SymmetricEncryptorDecryptor;
031import org.pgpainless.util.Passphrase;
032
033public class PGPainless {
034
035    /**
036     * Generate a new OpenPGP key ring.
037     * @return builder
038     */
039    public static KeyRingBuilder generateKeyRing() {
040        return new KeyRingBuilder();
041    }
042
043    /**
044     * Read an existing OpenPGP key ring.
045     * @return builder
046     */
047    public static KeyRingReader readKeyRing() {
048        return new KeyRingReader();
049    }
050
051    /**
052     * Create an {@link EncryptionStream}, which can be used to encrypt and/or sign data using OpenPGP.
053     * @return builder
054     */
055    public static EncryptionBuilder createEncryptor() {
056        return new EncryptionBuilder();
057    }
058
059    /**
060     * Create a {@link DecryptionStream}, which can be used to decrypt and/or verify data using OpenPGP.
061     * @return builder
062     */
063    public static DecryptionBuilder createDecryptor() {
064        return new DecryptionBuilder();
065    }
066
067    /**
068     * Encrypt some data symmetrically using OpenPGP and a password.
069     * The resulting data will be uncompressed and integrity protected.
070     *
071     * @param data input data.
072     * @param password password.
073     * @return symmetrically OpenPGP encrypted data.
074     * @throws IOException IO is dangerous.
075     * @throws PGPException PGP is brittle.
076     */
077    public static byte[] encryptWithPassword(@Nonnull byte[] data, @Nonnull Passphrase password, @Nonnull SymmetricKeyAlgorithm algorithm) throws IOException, PGPException {
078        return SymmetricEncryptorDecryptor.symmetricallyEncrypt(data, password,
079                algorithm, CompressionAlgorithm.UNCOMPRESSED);
080    }
081
082    /**
083     * Decrypt some symmetrically encrypted data using a password.
084     * The data is suspected to be integrity protected.
085     *
086     * @param data symmetrically OpenPGP encrypted data.
087     * @param password password.
088     * @return decrypted data.
089     * @throws IOException IO is dangerous.
090     * @throws PGPException PGP is brittle.
091     */
092    public static byte[] decryptWithPassword(@Nonnull byte[] data, @Nonnull Passphrase password) throws IOException, PGPException {
093        return SymmetricEncryptorDecryptor.symmetricallyDecrypt(data, password);
094    }
095}