Skip to content

Commit 6509a25

Browse files
committed
OpenPGPSignature: Add customizable toAsciiArmoredString() methods
1 parent e7570ab commit 6509a25

File tree

1 file changed

+64
-7
lines changed

1 file changed

+64
-7
lines changed

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPSignature.java

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,78 @@ protected String getType()
452452
public String toAsciiArmoredString()
453453
throws IOException
454454
{
455-
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
456-
ArmoredOutputStream.Builder aBuilder = ArmoredOutputStream.builder()
455+
return toAsciiArmoredString(PacketFormat.ROUNDTRIP);
456+
}
457+
458+
/**
459+
* Return an ASCII armored String representation of the signature.
460+
* If the signature contains issuer information, the fingerprint or key-id of the issuer will be added
461+
* to the ASCII armor as a comment header.
462+
*
463+
* @param packetFormat decide, which packet format to use when encoding the signature
464+
* @return ASCII armored signature
465+
* @throws IOException if the signature cannot be encoded
466+
*/
467+
public String toAsciiArmoredString(PacketFormat packetFormat)
468+
throws IOException
469+
{
470+
ArmoredOutputStream.Builder armorBuilder = ArmoredOutputStream.builder()
457471
.clearHeaders();
458472
if (getKeyIdentifier() != null)
459473
{
460-
aBuilder.addSplitMultilineComment(getKeyIdentifier().toPrettyPrint());
474+
armorBuilder.addSplitMultilineComment(getKeyIdentifier().toPrettyPrint());
461475
}
462-
ArmoredOutputStream aOut = aBuilder.build(bOut);
463-
BCPGOutputStream pOut = new BCPGOutputStream(aOut, PacketFormat.CURRENT);
464-
getSignature().encode(pOut);
465-
pOut.close();
476+
return toAsciiArmoredString(packetFormat, armorBuilder);
477+
}
478+
479+
/**
480+
* Return an ASCII armored String representation of the signature.
481+
* The ASCII armor can be configured using the passed {@link ArmoredOutputStream.Builder}.
482+
*
483+
* @param packetFormat decide, which packet format to use when encoding the signature
484+
* @param armorBuilder builder for the ASCII armored output stream
485+
* @return ASCII armored signature
486+
* @throws IOException if the signature cannot be encoded
487+
*/
488+
public String toAsciiArmoredString(PacketFormat packetFormat, ArmoredOutputStream.Builder armorBuilder)
489+
throws IOException
490+
{
491+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
492+
ArmoredOutputStream aOut = armorBuilder.build(bOut);
493+
aOut.write(getEncoded(packetFormat));
466494
aOut.close();
467495
return bOut.toString();
468496
}
469497

498+
/**
499+
* Return the binary encoding of the signature.
500+
*
501+
* @return binary encoding
502+
* @throws IOException if the signature cannot be encoded
503+
*/
504+
public byte[] getEncoded()
505+
throws IOException
506+
{
507+
return getEncoded(PacketFormat.ROUNDTRIP);
508+
}
509+
510+
/**
511+
* Return the binary encoding of the signature.
512+
*
513+
* @param packetFormat decide, which packet format to use when encoding the signature
514+
* @return binary encoding
515+
* @throws IOException if the signature cannot be encoded
516+
*/
517+
public byte[] getEncoded(PacketFormat packetFormat)
518+
throws IOException
519+
{
520+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
521+
BCPGOutputStream pOut = new BCPGOutputStream(bOut, packetFormat);
522+
signature.encode(pOut);
523+
pOut.close();
524+
return bOut.toByteArray();
525+
}
526+
470527
/**
471528
* {@link SignatureSubpacket} and the {@link OpenPGPSignature} that contains it.
472529
*/

0 commit comments

Comments
 (0)