Java Code Examples for org.bouncycastle.bcpg.ArmoredOutputStream#setHeader()

The following examples show how to use org.bouncycastle.bcpg.ArmoredOutputStream#setHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Encryptor.java    From jpgpj with MIT License 6 votes vote down vote up
/**
 * Wraps with stream that outputs ASCII-armored text - including configuring
 * its armor headers.
 *
 * @param meta The input plaintext {@link FileMetadata} - might be empty
 * (but not {@code null}).
 * @param out The {@link OutputStream} to wrap
 * @return The wrapped output stream - {@code null} if no wrapping.
 * @see #isAsciiArmored()
 * @see #isRemoveDefaultArmoredVersionHeader()
 * @see #setArmoredHeaders(Map) setArmoredHeaders
 * @see #addArmoredHeaders(Map) addArmoredHeaders
 * @see #updateArmoredHeader(String, String) updateArmoredHeader
 * @see #setArmorHeadersCallback(EncryptedAsciiArmorHeadersCallback)
 */
protected OutputStream armor(OutputStream out, FileMetadata meta) {
    if (!isAsciiArmored()) {
        return null;
    }

    ArmoredOutputStream aos = new ArmoredOutputStream(out);
    if (isRemoveDefaultArmoredVersionHeader()) {
        aos.setHeader(ArmoredOutputStream.VERSION_HDR, null);
    }

    // add the global headers - if any
    armoredHeaders.forEach((name, value) -> aos.setHeader(name, value));

    // see if user wants to manipulate the headers
    EncryptedAsciiArmorHeadersCallback callback = getArmorHeadersCallback();
    if (callback != null) {
        EncryptedAsciiArmorHeadersManipulator manipulator =
            EncryptedAsciiArmorHeadersManipulator.wrap(aos);
        callback.prepareAsciiArmoredHeaders(this, meta, manipulator);
    }

    return aos;
}
 
Example 2
Source File: AbstractSecretKeySigningService.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void printPublicKey ( final OutputStream out ) throws IOException
{
    final ArmoredOutputStream armoredOutput = new ArmoredOutputStream ( out );
    armoredOutput.setHeader ( "Version", VersionInformation.VERSIONED_PRODUCT );

    final PGPPublicKey pubKey = this.secretKey.getPublicKey ();
    pubKey.encode ( new BCPGOutputStream ( armoredOutput ) );
    armoredOutput.close ();
}
 
Example 3
Source File: AbstractSecretKeySigningService.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void sign ( final InputStream in, final OutputStream out, final boolean inline ) throws Exception
{
    final int digest = HashAlgorithmTags.SHA1;
    final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator ( new BcPGPContentSignerBuilder ( this.privateKey.getPublicKeyPacket ().getAlgorithm (), digest ) );

    if ( inline )
    {
        signatureGenerator.init ( PGPSignature.CANONICAL_TEXT_DOCUMENT, this.privateKey );
    }
    else
    {
        signatureGenerator.init ( PGPSignature.BINARY_DOCUMENT, this.privateKey );
    }

    final ArmoredOutputStream armoredOutput = new ArmoredOutputStream ( out );
    armoredOutput.setHeader ( "Version", VersionInformation.VERSIONED_PRODUCT );

    if ( inline )
    {
        armoredOutput.beginClearText ( digest );

        final LineNumberReader lnr = new LineNumberReader ( new InputStreamReader ( in, StandardCharsets.UTF_8 ) );

        String line;
        while ( ( line = lnr.readLine () ) != null )
        {
            if ( lnr.getLineNumber () > 1 )
            {
                signatureGenerator.update ( NL_DATA );
            }

            final byte[] data = trimTrailing ( line ).getBytes ( StandardCharsets.UTF_8 );

            if ( inline )
            {
                armoredOutput.write ( data );
                armoredOutput.write ( NL_DATA );
            }
            signatureGenerator.update ( data );
        }

        armoredOutput.endClearText ();
    }
    else
    {

        final byte[] buffer = new byte[4096];
        int rc;
        while ( ( rc = in.read ( buffer ) ) >= 0 )
        {
            signatureGenerator.update ( buffer, 0, rc );
        }
    }

    final PGPSignature signature = signatureGenerator.generate ();
    signature.encode ( new BCPGOutputStream ( armoredOutput ) );

    armoredOutput.close ();
}
 
Example 4
Source File: EncryptedAsciiArmorHeadersManipulator.java    From jpgpj with MIT License 2 votes vote down vote up
/**
 * Wraps an {@link ArmoredOutputStream}
 *
 * @param aos The stream to wrap - ignored if {@code null}
 * @return The manipulator wrapping
 */
static EncryptedAsciiArmorHeadersManipulator wrap(ArmoredOutputStream aos) {
    return (aos == null) ? EMPTY : (name, value) -> aos.setHeader(name, value);
}