001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package sop.operation;
006
007import java.io.ByteArrayInputStream;
008import java.io.IOException;
009import java.io.InputStream;
010
011import sop.Ready;
012import sop.enums.EncryptAs;
013import sop.exception.SOPGPException;
014
015public interface Encrypt {
016
017    /**
018     * Disable ASCII armor encoding.
019     *
020     * @return builder instance
021     */
022    Encrypt noArmor();
023
024    /**
025     * Sets encryption mode.
026     *
027     * @param mode mode
028     * @return builder instance
029     */
030    Encrypt mode(EncryptAs mode)
031            throws SOPGPException.UnsupportedOption;
032
033    /**
034     * Adds the signer key.
035     *
036     * @param key input stream containing the encoded signer key
037     * @return builder instance
038     */
039    Encrypt signWith(InputStream key)
040            throws SOPGPException.KeyIsProtected,
041            SOPGPException.CertCannotSign,
042            SOPGPException.UnsupportedAsymmetricAlgo,
043            SOPGPException.BadData;
044
045    /**
046     * Adds the signer key.
047     *
048     * @param key byte array containing the encoded signer key
049     * @return builder instance
050     */
051    default Encrypt signWith(byte[] key)
052            throws SOPGPException.KeyIsProtected,
053            SOPGPException.CertCannotSign,
054            SOPGPException.UnsupportedAsymmetricAlgo,
055            SOPGPException.BadData {
056        return signWith(new ByteArrayInputStream(key));
057    }
058
059    /**
060     * Encrypt with the given password.
061     *
062     * @param password password
063     * @return builder instance
064     */
065    Encrypt withPassword(String password)
066            throws SOPGPException.PasswordNotHumanReadable,
067            SOPGPException.UnsupportedOption;
068
069    /**
070     * Encrypt with the given cert.
071     *
072     * @param cert input stream containing the encoded cert.
073     * @return builder instance
074     */
075    Encrypt withCert(InputStream cert)
076            throws SOPGPException.CertCannotEncrypt,
077            SOPGPException.UnsupportedAsymmetricAlgo,
078            SOPGPException.BadData;
079
080    /**
081     * Encrypt with the given cert.
082     *
083     * @param cert byte array containing the encoded cert.
084     * @return builder instance
085     */
086    default Encrypt withCert(byte[] cert)
087            throws SOPGPException.CertCannotEncrypt,
088            SOPGPException.UnsupportedAsymmetricAlgo,
089            SOPGPException.BadData {
090        return withCert(new ByteArrayInputStream(cert));
091    }
092
093    /**
094     * Encrypt the given data yielding the ciphertext.
095     * @param plaintext plaintext
096     * @return input stream containing the ciphertext
097     */
098    Ready plaintext(InputStream plaintext)
099        throws IOException;
100
101    /**
102     * Encrypt the given data yielding the ciphertext.
103     * @param plaintext plaintext
104     * @return input stream containing the ciphertext
105     */
106    default Ready plaintext(byte[] plaintext) throws IOException {
107        return plaintext(new ByteArrayInputStream(plaintext));
108    }
109}