mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
SOP: Add convenience methods to deal with byte arrays
This commit is contained in:
parent
32f3f0246e
commit
15736586dd
25 changed files with 451 additions and 128 deletions
|
@ -4,6 +4,13 @@
|
|||
|
||||
package sop;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Tuple of a byte array and associated result object.
|
||||
* @param <T> type of result
|
||||
*/
|
||||
public class ByteArrayAndResult<T> {
|
||||
|
||||
private final byte[] bytes;
|
||||
|
@ -14,11 +21,30 @@ public class ByteArrayAndResult<T> {
|
|||
this.result = result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the byte array part.
|
||||
*
|
||||
* @return bytes
|
||||
*/
|
||||
public byte[] getBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the result part.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
public T getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the byte array part as an {@link InputStream}.
|
||||
*
|
||||
* @return input stream
|
||||
*/
|
||||
public InputStream getInputStream() {
|
||||
return new ByteArrayInputStream(getBytes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
|
||||
package sop;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public abstract class Ready {
|
||||
|
@ -30,4 +32,14 @@ public abstract class Ready {
|
|||
writeTo(bytes);
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an input stream containing the data.
|
||||
*
|
||||
* @return input stream
|
||||
* @throws IOException in case of an IO error
|
||||
*/
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(getBytes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,11 +20,20 @@ public abstract class ReadyWithResult<T> {
|
|||
* @return result, eg. signatures
|
||||
*
|
||||
* @throws IOException in case of an IO error
|
||||
* @throws SOPGPException.NoSignature
|
||||
* @throws SOPGPException.NoSignature if there are no valid signatures found
|
||||
*/
|
||||
public abstract T writeTo(OutputStream outputStream) throws IOException, SOPGPException.NoSignature;
|
||||
|
||||
public ByteArrayAndResult<T> toBytes() throws IOException, SOPGPException.NoSignature {
|
||||
/**
|
||||
* Return the data as a {@link ByteArrayAndResult}.
|
||||
* Calling {@link ByteArrayAndResult#getBytes()} will give you access to the data as byte array, while
|
||||
* {@link ByteArrayAndResult#getResult()} will grant access to the appended result.
|
||||
*
|
||||
* @return byte array and result
|
||||
* @throws IOException in case of an IO error
|
||||
* @throws SOPGPException.NoSignature if there are no valid signatures found
|
||||
*/
|
||||
public ByteArrayAndResult<T> toByteArrayAndResult() throws IOException, SOPGPException.NoSignature {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
T result = writeTo(bytes);
|
||||
return new ByteArrayAndResult<>(bytes.toByteArray(), result);
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import sop.Ready;
|
||||
|
@ -27,4 +28,14 @@ public interface Armor {
|
|||
* @return armored data
|
||||
*/
|
||||
Ready data(InputStream data) throws SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Armor the provided data.
|
||||
*
|
||||
* @param data unarmored OpenPGP data
|
||||
* @return armored data
|
||||
*/
|
||||
default Ready data(byte[] data) throws SOPGPException.BadData {
|
||||
return data(new ByteArrayInputStream(data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
@ -19,4 +20,14 @@ public interface Dearmor {
|
|||
* @return input stream of unarmored data
|
||||
*/
|
||||
Ready data(InputStream data) throws SOPGPException.BadData, IOException;
|
||||
|
||||
/**
|
||||
* Dearmor armored OpenPGP data.
|
||||
*
|
||||
* @param data armored OpenPGP data
|
||||
* @return input stream of unarmored data
|
||||
*/
|
||||
default Ready data(byte[] data) throws SOPGPException.BadData, IOException {
|
||||
return data(new ByteArrayInputStream(data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
@ -43,6 +44,17 @@ public interface Decrypt {
|
|||
throws SOPGPException.BadData,
|
||||
IOException;
|
||||
|
||||
/**
|
||||
* Adds the verification cert.
|
||||
*
|
||||
* @param cert byte array containing the cert
|
||||
* @return builder instance
|
||||
*/
|
||||
default Decrypt verifyWithCert(byte[] cert)
|
||||
throws SOPGPException.BadData, IOException {
|
||||
return verifyWithCert(new ByteArrayInputStream(cert));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to decrypt with the given session key.
|
||||
*
|
||||
|
@ -73,6 +85,19 @@ public interface Decrypt {
|
|||
SOPGPException.BadData,
|
||||
SOPGPException.UnsupportedAsymmetricAlgo;
|
||||
|
||||
/**
|
||||
* Adds the decryption key.
|
||||
*
|
||||
* @param key byte array containing the key
|
||||
* @return builder instance
|
||||
*/
|
||||
default Decrypt withKey(byte[] key)
|
||||
throws SOPGPException.KeyIsProtected,
|
||||
SOPGPException.BadData,
|
||||
SOPGPException.UnsupportedAsymmetricAlgo {
|
||||
return withKey(new ByteArrayInputStream(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts the given ciphertext, returning verification results and plaintext.
|
||||
* @param ciphertext ciphertext
|
||||
|
@ -80,4 +105,14 @@ public interface Decrypt {
|
|||
*/
|
||||
ReadyWithResult<DecryptionResult> ciphertext(InputStream ciphertext)
|
||||
throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt;
|
||||
|
||||
/**
|
||||
* Decrypts the given ciphertext, returning verification results and plaintext.
|
||||
* @param ciphertext ciphertext
|
||||
* @return ready with result
|
||||
*/
|
||||
default ReadyWithResult<DecryptionResult> ciphertext(byte[] ciphertext)
|
||||
throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt {
|
||||
return ciphertext(new ByteArrayInputStream(ciphertext));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
@ -12,8 +13,29 @@ import sop.Signatures;
|
|||
|
||||
public interface DetachInbandSignatureAndMessage {
|
||||
|
||||
/**
|
||||
* Do not wrap the signatures in ASCII armor.
|
||||
* @return builder
|
||||
*/
|
||||
DetachInbandSignatureAndMessage noArmor();
|
||||
|
||||
/**
|
||||
* Detach the provided cleartext signed message from its signatures.
|
||||
*
|
||||
* @param messageInputStream input stream containing the signed message
|
||||
* @return result containing the detached message
|
||||
* @throws IOException in case of an IO error
|
||||
*/
|
||||
ReadyWithResult<Signatures> message(InputStream messageInputStream) throws IOException;
|
||||
|
||||
/**
|
||||
* Detach the provided cleartext signed message from its signatures.
|
||||
*
|
||||
* @param message byte array containing the signed message
|
||||
* @return result containing the detached message
|
||||
* @throws IOException in case of an IO error
|
||||
*/
|
||||
default ReadyWithResult<Signatures> message(byte[] message) throws IOException {
|
||||
return message(new ByteArrayInputStream(message));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
@ -41,6 +42,20 @@ public interface Encrypt {
|
|||
SOPGPException.UnsupportedAsymmetricAlgo,
|
||||
SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Adds the signer key.
|
||||
*
|
||||
* @param key byte array containing the encoded signer key
|
||||
* @return builder instance
|
||||
*/
|
||||
default Encrypt signWith(byte[] key)
|
||||
throws SOPGPException.KeyIsProtected,
|
||||
SOPGPException.CertCannotSign,
|
||||
SOPGPException.UnsupportedAsymmetricAlgo,
|
||||
SOPGPException.BadData {
|
||||
return signWith(new ByteArrayInputStream(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt with the given password.
|
||||
*
|
||||
|
@ -62,6 +77,19 @@ public interface Encrypt {
|
|||
SOPGPException.UnsupportedAsymmetricAlgo,
|
||||
SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Encrypt with the given cert.
|
||||
*
|
||||
* @param cert byte array containing the encoded cert.
|
||||
* @return builder instance
|
||||
*/
|
||||
default Encrypt withCert(byte[] cert)
|
||||
throws SOPGPException.CertCannotEncrypt,
|
||||
SOPGPException.UnsupportedAsymmetricAlgo,
|
||||
SOPGPException.BadData {
|
||||
return withCert(new ByteArrayInputStream(cert));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt the given data yielding the ciphertext.
|
||||
* @param plaintext plaintext
|
||||
|
@ -69,4 +97,13 @@ public interface Encrypt {
|
|||
*/
|
||||
Ready plaintext(InputStream plaintext)
|
||||
throws IOException;
|
||||
|
||||
/**
|
||||
* Encrypt the given data yielding the ciphertext.
|
||||
* @param plaintext plaintext
|
||||
* @return input stream containing the ciphertext
|
||||
*/
|
||||
default Ready plaintext(byte[] plaintext) throws IOException {
|
||||
return plaintext(new ByteArrayInputStream(plaintext));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
@ -23,7 +24,17 @@ public interface ExtractCert {
|
|||
* Extract the cert from the provided key.
|
||||
*
|
||||
* @param keyInputStream input stream containing the encoding of an OpenPGP key
|
||||
* @return input stream containing the encoding of the keys cert
|
||||
* @return result containing the encoding of the keys cert
|
||||
*/
|
||||
Ready key(InputStream keyInputStream) throws IOException, SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Extract the cert from the provided key.
|
||||
*
|
||||
* @param key byte array containing the encoding of an OpenPGP key
|
||||
* @return result containing the encoding of the keys cert
|
||||
*/
|
||||
default Ready key(byte[] key) throws IOException, SOPGPException.BadData {
|
||||
return key(new ByteArrayInputStream(key));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
@ -37,6 +38,16 @@ public interface Sign {
|
|||
*/
|
||||
Sign key(InputStream key) throws SOPGPException.KeyIsProtected, SOPGPException.BadData, IOException;
|
||||
|
||||
/**
|
||||
* Adds the signer key.
|
||||
*
|
||||
* @param key byte array containing encoded key
|
||||
* @return builder instance
|
||||
*/
|
||||
default Sign key(byte[] key) throws SOPGPException.KeyIsProtected, SOPGPException.BadData, IOException {
|
||||
return key(new ByteArrayInputStream(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs data.
|
||||
*
|
||||
|
@ -44,4 +55,14 @@ public interface Sign {
|
|||
* @return ready
|
||||
*/
|
||||
Ready data(InputStream data) throws IOException, SOPGPException.ExpectedText;
|
||||
|
||||
/**
|
||||
* Signs data.
|
||||
*
|
||||
* @param data byte array containing data
|
||||
* @return ready
|
||||
*/
|
||||
default Ready data(byte[] data) throws IOException, SOPGPException.ExpectedText {
|
||||
return data(new ByteArrayInputStream(data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
|
@ -35,6 +36,16 @@ public interface Verify extends VerifySignatures {
|
|||
*/
|
||||
Verify cert(InputStream cert) throws SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Adds the verification cert.
|
||||
*
|
||||
* @param cert byte array containing the encoded cert
|
||||
* @return builder instance
|
||||
*/
|
||||
default Verify cert(byte[] cert) throws SOPGPException.BadData {
|
||||
return cert(new ByteArrayInputStream(cert));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the signatures.
|
||||
* @param signatures input stream containing encoded, detached signatures.
|
||||
|
@ -43,4 +54,14 @@ public interface Verify extends VerifySignatures {
|
|||
*/
|
||||
VerifySignatures signatures(InputStream signatures) throws SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Provides the signatures.
|
||||
* @param signatures byte array containing encoded, detached signatures.
|
||||
*
|
||||
* @return builder instance
|
||||
*/
|
||||
default VerifySignatures signatures(byte[] signatures) throws SOPGPException.BadData {
|
||||
return signatures(new ByteArrayInputStream(signatures));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package sop.operation;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
@ -13,5 +14,27 @@ import sop.exception.SOPGPException;
|
|||
|
||||
public interface VerifySignatures {
|
||||
|
||||
/**
|
||||
* Provide the signed data (without signatures).
|
||||
*
|
||||
* @param data signed data
|
||||
* @return list of signature verifications
|
||||
* @throws IOException in case of an IO error
|
||||
* @throws SOPGPException.NoSignature when no signature is found
|
||||
* @throws SOPGPException.BadData when the data is invalid OpenPGP data
|
||||
*/
|
||||
List<Verification> data(InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData;
|
||||
|
||||
/**
|
||||
* Provide the signed data (without signatures).
|
||||
*
|
||||
* @param data signed data
|
||||
* @return list of signature verifications
|
||||
* @throws IOException in case of an IO error
|
||||
* @throws SOPGPException.NoSignature when no signature is found
|
||||
* @throws SOPGPException.BadData when the data is invalid OpenPGP data
|
||||
*/
|
||||
default List<Verification> data(byte[] data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
|
||||
return data(new ByteArrayInputStream(data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ public class ReadyWithResultTest {
|
|||
}
|
||||
};
|
||||
|
||||
ByteArrayAndResult<List<Verification>> bytesAndResult = readyWithResult.toBytes();
|
||||
ByteArrayAndResult<List<Verification>> bytesAndResult = readyWithResult.toByteArrayAndResult();
|
||||
assertArrayEquals(data, bytesAndResult.getBytes());
|
||||
assertEquals(result, bytesAndResult.getResult());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue