mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 12:52:07 +01:00
Add SignatureUtils.readSignatures(byte[])
This commit is contained in:
parent
1a274bf91b
commit
e19acb667c
2 changed files with 31 additions and 17 deletions
|
@ -217,14 +217,28 @@ public final class SignatureUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse an ASCII encoded list of OpenPGP signatures into a {@link PGPSignatureList}.
|
* Parse an ASCII encoded list of OpenPGP signatures into a {@link PGPSignatureList}
|
||||||
|
* and return it as a {@link List}.
|
||||||
*
|
*
|
||||||
* @param encodedSignatures ASCII armored signature list
|
* @param encodedSignatures ASCII armored signature list
|
||||||
* @return signature list
|
* @return signature list
|
||||||
* @throws IOException if the signatures cannot be read
|
* @throws IOException if the signatures cannot be read
|
||||||
*/
|
*/
|
||||||
public static List<PGPSignature> readSignatures(String encodedSignatures) throws IOException, PGPException {
|
public static List<PGPSignature> readSignatures(String encodedSignatures) throws IOException, PGPException {
|
||||||
InputStream inputStream = new ByteArrayInputStream(encodedSignatures.getBytes(Charset.forName("UTF8")));
|
byte[] bytes = encodedSignatures.getBytes(Charset.forName("UTF8"));
|
||||||
|
return readSignatures(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a single, or a list of {@link PGPSignature PGPSignatures} and return them as a {@link List}.
|
||||||
|
*
|
||||||
|
* @param encodedSignatures ASCII armored or binary signatures
|
||||||
|
* @return signatures
|
||||||
|
* @throws IOException if the signatures cannot be read
|
||||||
|
* @throws PGPException in case of an OpenPGP error
|
||||||
|
*/
|
||||||
|
public static List<PGPSignature> readSignatures(byte[] encodedSignatures) throws IOException, PGPException {
|
||||||
|
InputStream inputStream = new ByteArrayInputStream(encodedSignatures);
|
||||||
return readSignatures(inputStream);
|
return readSignatures(inputStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,13 +44,13 @@ import org.pgpainless.util.TestUtils;
|
||||||
|
|
||||||
public class CleartextSignatureVerificationTest {
|
public class CleartextSignatureVerificationTest {
|
||||||
|
|
||||||
public static final String MESSAGE_BODY = "Ah, Juliet, if the measure of thy joy\n" +
|
public static final byte[] MESSAGE_BODY = ("Ah, Juliet, if the measure of thy joy\n" +
|
||||||
"Be heaped like mine, and that thy skill be more\n" +
|
"Be heaped like mine, and that thy skill be more\n" +
|
||||||
"To blazon it, then sweeten with thy breath\n" +
|
"To blazon it, then sweeten with thy breath\n" +
|
||||||
"This neighbor air, and let rich music’s tongue\n" +
|
"This neighbor air, and let rich music’s tongue\n" +
|
||||||
"Unfold the imagined happiness that both\n" +
|
"Unfold the imagined happiness that both\n" +
|
||||||
"Receive in either by this dear encounter.";
|
"Receive in either by this dear encounter.").getBytes(StandardCharsets.UTF_8);
|
||||||
public static final String MESSAGE_SIGNED = "-----BEGIN PGP SIGNED MESSAGE-----\n" +
|
public static final byte[] MESSAGE_SIGNED = ("-----BEGIN PGP SIGNED MESSAGE-----\n" +
|
||||||
"Hash: SHA512\n" +
|
"Hash: SHA512\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"Ah, Juliet, if the measure of thy joy\n" +
|
"Ah, Juliet, if the measure of thy joy\n" +
|
||||||
|
@ -65,14 +65,14 @@ public class CleartextSignatureVerificationTest {
|
||||||
"DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" +
|
"DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" +
|
||||||
"ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" +
|
"ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" +
|
||||||
"=Z2SO\n" +
|
"=Z2SO\n" +
|
||||||
"-----END PGP SIGNATURE-----";
|
"-----END PGP SIGNATURE-----").getBytes(StandardCharsets.UTF_8);
|
||||||
public static final String SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n" +
|
public static final byte[] SIGNATURE = ("-----BEGIN PGP SIGNATURE-----\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"iHUEARMKAB0WIQRPZlxNwsRmC8ZCXkFXNuaTGs83DAUCYJ/x5gAKCRBXNuaTGs83\n" +
|
"iHUEARMKAB0WIQRPZlxNwsRmC8ZCXkFXNuaTGs83DAUCYJ/x5gAKCRBXNuaTGs83\n" +
|
||||||
"DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" +
|
"DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" +
|
||||||
"ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" +
|
"ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" +
|
||||||
"=Z2SO\n" +
|
"=Z2SO\n" +
|
||||||
"-----END PGP SIGNATURE-----";
|
"-----END PGP SIGNATURE-----").getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cleartextSignVerification_InMemoryMultiPassStrategy() throws IOException, PGPException {
|
public void cleartextSignVerification_InMemoryMultiPassStrategy() throws IOException, PGPException {
|
||||||
|
@ -80,14 +80,14 @@ public class CleartextSignatureVerificationTest {
|
||||||
|
|
||||||
InMemoryMultiPassStrategy multiPassStrategy = MultiPassStrategy.keepMessageInMemory();
|
InMemoryMultiPassStrategy multiPassStrategy = MultiPassStrategy.keepMessageInMemory();
|
||||||
CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage()
|
CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage()
|
||||||
.onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED.getBytes(StandardCharsets.UTF_8)))
|
.onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED))
|
||||||
.withStrategy(multiPassStrategy)
|
.withStrategy(multiPassStrategy)
|
||||||
.verifyWith(signingKeys);
|
.verifyWith(signingKeys);
|
||||||
|
|
||||||
PGPSignature signature = processor.process();
|
PGPSignature signature = processor.process();
|
||||||
|
|
||||||
assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID());
|
assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID());
|
||||||
assertArrayEquals(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8), multiPassStrategy.getBytes());
|
assertArrayEquals(MESSAGE_BODY, multiPassStrategy.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -98,7 +98,7 @@ public class CleartextSignatureVerificationTest {
|
||||||
File file = new File(tempDir, "file");
|
File file = new File(tempDir, "file");
|
||||||
MultiPassStrategy multiPassStrategy = MultiPassStrategy.writeMessageToFile(file);
|
MultiPassStrategy multiPassStrategy = MultiPassStrategy.writeMessageToFile(file);
|
||||||
CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage()
|
CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage()
|
||||||
.onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED.getBytes(StandardCharsets.UTF_8)))
|
.onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED))
|
||||||
.withStrategy(multiPassStrategy)
|
.withStrategy(multiPassStrategy)
|
||||||
.verifyWith(signingKeys);
|
.verifyWith(signingKeys);
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ public class CleartextSignatureVerificationTest {
|
||||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||||
Streams.pipeAll(fileIn, bytes);
|
Streams.pipeAll(fileIn, bytes);
|
||||||
fileIn.close();
|
fileIn.close();
|
||||||
assertArrayEquals(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8), bytes.toByteArray());
|
assertArrayEquals(MESSAGE_BODY, bytes.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -119,7 +119,7 @@ public class CleartextSignatureVerificationTest {
|
||||||
PGPSignature signature = SignatureUtils.readSignatures(SIGNATURE).get(0);
|
PGPSignature signature = SignatureUtils.readSignatures(SIGNATURE).get(0);
|
||||||
PGPPublicKey signingKey = signingKeys.getPublicKey(signature.getKeyID());
|
PGPPublicKey signingKey = signingKeys.getPublicKey(signature.getKeyID());
|
||||||
|
|
||||||
SignatureVerifier.initializeSignatureAndUpdateWithSignedData(signature, new ByteArrayInputStream(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8)), signingKey);
|
SignatureVerifier.initializeSignatureAndUpdateWithSignedData(signature, new ByteArrayInputStream(MESSAGE_BODY), signingKey);
|
||||||
|
|
||||||
CertificateValidator.validateCertificateAndVerifyInitializedSignature(signature, signingKeys, PGPainless.getPolicy());
|
CertificateValidator.validateCertificateAndVerifyInitializedSignature(signature, signingKeys, PGPainless.getPolicy());
|
||||||
}
|
}
|
||||||
|
@ -128,9 +128,9 @@ public class CleartextSignatureVerificationTest {
|
||||||
// CHECKSTYLE:OFF
|
// CHECKSTYLE:OFF
|
||||||
PGPPublicKeyRing keys = TestKeys.getEmilPublicKeyRing();
|
PGPPublicKeyRing keys = TestKeys.getEmilPublicKeyRing();
|
||||||
System.out.println(ArmorUtils.toAsciiArmoredString(keys));
|
System.out.println(ArmorUtils.toAsciiArmoredString(keys));
|
||||||
System.out.println(MESSAGE_SIGNED);
|
System.out.println(new String(MESSAGE_SIGNED));
|
||||||
System.out.println(MESSAGE_BODY);
|
System.out.println(new String(MESSAGE_BODY));
|
||||||
System.out.println(SIGNATURE);
|
System.out.println(new String(SIGNATURE));
|
||||||
// CHECKSTYLE:ON
|
// CHECKSTYLE:ON
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +143,7 @@ public class CleartextSignatureVerificationTest {
|
||||||
.addVerificationOfDetachedSignature(signature);
|
.addVerificationOfDetachedSignature(signature);
|
||||||
|
|
||||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||||
.onInputStream(new ByteArrayInputStream(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8)))
|
.onInputStream(new ByteArrayInputStream(MESSAGE_BODY))
|
||||||
.withOptions(options);
|
.withOptions(options);
|
||||||
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
Loading…
Reference in a new issue