Add InlineSign API

This commit is contained in:
Paul Schaub 2022-05-24 21:44:46 +02:00
parent e8d7d1b5f4
commit 580c3af350
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 135 additions and 82 deletions

View file

@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// 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,
}

View file

@ -0,0 +1,92 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// 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<T> {
/**
* 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<SigningResult> 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<SigningResult> data(byte[] data) throws IOException, SOPGPException.ExpectedText {
return data(new ByteArrayInputStream(data));
}
}

View file

@ -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<InlineSign> {
/**
* 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;
}

View file

@ -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<Sign> {
/**
* 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<SigningResult> 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<SigningResult> data(byte[] data) throws IOException, SOPGPException.ExpectedText {
return data(new ByteArrayInputStream(data));
}
}