diff --git a/sop-java/src/main/java/sop/enums/InlineSignAs.java b/sop-java/src/main/java/sop/enums/InlineSignAs.java new file mode 100644 index 0000000..b0b55dc --- /dev/null +++ b/sop-java/src/main/java/sop/enums/InlineSignAs.java @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package sop.enums; + +public enum InlineSignAs { + + /** + * Signature is made over the binary message. + */ + Binary, + + /** + * Signature is made over the message in text mode. + */ + Text, + + /** + * Signature is made using the Cleartext Signature Framework. + */ + CleartextSigned, +} + diff --git a/sop-java/src/main/java/sop/operation/AbstractSign.java b/sop-java/src/main/java/sop/operation/AbstractSign.java new file mode 100644 index 0000000..4fd0b8a --- /dev/null +++ b/sop-java/src/main/java/sop/operation/AbstractSign.java @@ -0,0 +1,92 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package sop.operation; + +import sop.ReadyWithResult; +import sop.SigningResult; +import sop.exception.SOPGPException; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; + +public interface AbstractSign { + + /** + * Disable ASCII armor encoding. + * + * @return builder instance + */ + Sign noArmor(); + + /** + * Add one or more signing keys. + * + * @param key input stream containing encoded keys + * @return builder instance + * + * @throws sop.exception.SOPGPException.KeyIsProtected if the key is password protected + * @throws sop.exception.SOPGPException.BadData if the {@link InputStream} does not contain an OpenPGP key + * @throws IOException in case of an IO error + */ + T key(InputStream key) throws SOPGPException.KeyIsProtected, SOPGPException.BadData, IOException; + + /** + * Add one or more signing keys. + * + * @param key byte array containing encoded keys + * @return builder instance + * + * @throws sop.exception.SOPGPException.KeyIsProtected if the key is password protected + * @throws sop.exception.SOPGPException.BadData if the byte array does not contain an OpenPGP key + * @throws IOException in case of an IO error + */ + default T key(byte[] key) throws SOPGPException.KeyIsProtected, SOPGPException.BadData, IOException { + return key(new ByteArrayInputStream(key)); + } + + /** + * Provide the decryption password for the secret key. + * + * @param password password + * @return builder instance + */ + default T withKeyPassword(String password) { + return withKeyPassword(password.getBytes(Charset.forName("UTF8"))); + } + + /** + * Provide the decryption password for the secret key. + * + * @param password password + * @return builder instance + */ + T withKeyPassword(byte[] password); + + /** + * Signs data. + * + * @param data input stream containing data + * @return ready + * + * @throws IOException in case of an IO error + * @throws sop.exception.SOPGPException.ExpectedText if text data was expected, but binary data was encountered + */ + ReadyWithResult data(InputStream data) throws IOException, SOPGPException.ExpectedText; + + /** + * Signs data. + * + * @param data byte array containing data + * @return ready + * + * @throws IOException in case of an IO error + * @throws sop.exception.SOPGPException.ExpectedText if text data was expected, but binary data was encountered + */ + default ReadyWithResult data(byte[] data) throws IOException, SOPGPException.ExpectedText { + return data(new ByteArrayInputStream(data)); + } +} diff --git a/sop-java/src/main/java/sop/operation/InlineSign.java b/sop-java/src/main/java/sop/operation/InlineSign.java index ed1f90b..17bf80f 100644 --- a/sop-java/src/main/java/sop/operation/InlineSign.java +++ b/sop-java/src/main/java/sop/operation/InlineSign.java @@ -4,6 +4,22 @@ package sop.operation; -public interface InlineSign { +import sop.enums.InlineSignAs; +import sop.exception.SOPGPException; + +import java.io.InputStream; + +public interface InlineSign extends AbstractSign { + + /** + * Sets the signature mode. + * Note: This method has to be called before {@link #key(InputStream)} is called. + * + * @param mode signature mode + * @return builder instance + * + * @throws sop.exception.SOPGPException.UnsupportedOption if this option is not supported + */ + Sign mode(InlineSignAs mode) throws SOPGPException.UnsupportedOption; } diff --git a/sop-java/src/main/java/sop/operation/Sign.java b/sop-java/src/main/java/sop/operation/Sign.java index f02e709..28a8f72 100644 --- a/sop-java/src/main/java/sop/operation/Sign.java +++ b/sop-java/src/main/java/sop/operation/Sign.java @@ -4,24 +4,12 @@ package sop.operation; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -import sop.ReadyWithResult; -import sop.SigningResult; import sop.enums.SignAs; import sop.exception.SOPGPException; -public interface Sign { +import java.io.InputStream; - /** - * Disable ASCII armor encoding. - * - * @return builder instance - */ - Sign noArmor(); +public interface Sign extends AbstractSign { /** * Sets the signature mode. @@ -34,71 +22,4 @@ public interface Sign { */ Sign mode(SignAs mode) throws SOPGPException.UnsupportedOption; - /** - * Add one or more signing keys. - * - * @param key input stream containing encoded keys - * @return builder instance - * - * @throws sop.exception.SOPGPException.KeyIsProtected if the key is password protected - * @throws sop.exception.SOPGPException.BadData if the {@link InputStream} does not contain an OpenPGP key - * @throws IOException in case of an IO error - */ - Sign key(InputStream key) throws SOPGPException.KeyIsProtected, SOPGPException.BadData, IOException; - - /** - * Add one or more signing keys. - * - * @param key byte array containing encoded keys - * @return builder instance - * - * @throws sop.exception.SOPGPException.KeyIsProtected if the key is password protected - * @throws sop.exception.SOPGPException.BadData if the byte array does not contain an OpenPGP key - * @throws IOException in case of an IO error - */ - default Sign key(byte[] key) throws SOPGPException.KeyIsProtected, SOPGPException.BadData, IOException { - return key(new ByteArrayInputStream(key)); - } - - /** - * Provide the decryption password for the secret key. - * - * @param password password - * @return builder instance - */ - default Sign withKeyPassword(String password) { - return withKeyPassword(password.getBytes(Charset.forName("UTF8"))); - } - - /** - * Provide the decryption password for the secret key. - * - * @param password password - * @return builder instance - */ - Sign withKeyPassword(byte[] password); - - /** - * Signs data. - * - * @param data input stream containing data - * @return ready - * - * @throws IOException in case of an IO error - * @throws sop.exception.SOPGPException.ExpectedText if text data was expected, but binary data was encountered - */ - ReadyWithResult data(InputStream data) throws IOException, SOPGPException.ExpectedText; - - /** - * Signs data. - * - * @param data byte array containing data - * @return ready - * - * @throws IOException in case of an IO error - * @throws sop.exception.SOPGPException.ExpectedText if text data was expected, but binary data was encountered - */ - default ReadyWithResult data(byte[] data) throws IOException, SOPGPException.ExpectedText { - return data(new ByteArrayInputStream(data)); - } }