Example usage for org.bouncycastle.openpgp PGPSignature encode

List of usage examples for org.bouncycastle.openpgp PGPSignature encode

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPSignature encode.

Prototype

public void encode(OutputStream outStream) throws IOException 

Source Link

Usage

From source file:com.github.sannies.nexusaptplugin.sign.PGPSigner.java

License:Apache License

/**
 * Creates a clear sign signature over the input data. (Not detached)
 *
 * @param input      the content to be signed
 * @param output     the output destination of the signature
 *//*from   w  w  w  .ja v a 2s. c o  m*/
public void clearSign(InputStream input, OutputStream output)
        throws IOException, PGPException, GeneralSecurityException {
    int digest = PGPUtil.SHA1;

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), digest));
    signatureGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privateKey);

    ArmoredOutputStream armoredOutput = new ArmoredOutputStream(output);
    armoredOutput.beginClearText(digest);

    BufferedReader reader = new BufferedReader(new InputStreamReader(input));

    String line;
    while ((line = reader.readLine()) != null) {
        // trailing spaces must be removed for signature calculation (see http://tools.ietf.org/html/rfc4880#section-7.1)
        byte[] data = trim(line).getBytes("UTF-8");

        armoredOutput.write(data);
        armoredOutput.write(EOL);

        signatureGenerator.update(data);
        signatureGenerator.update(EOL);
    }

    armoredOutput.endClearText();

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

    armoredOutput.close();
}

From source file:com.google.gerrit.gpg.PushCertificateCheckerTest.java

License:Apache License

private PushCertificate newSignedCert(String nonce, TestKey signingKey) throws Exception {
    PushCertificateIdent ident = new PushCertificateIdent(signingKey.getFirstUserId(),
            System.currentTimeMillis(), -7 * 60);
    String payload = "certificate version 0.1\n" + "pusher " + ident.getRaw() + "\n"
            + "pushee test://localhost/repo.git\n" + "nonce " + nonce + "\n" + "\n"
            + "0000000000000000000000000000000000000000" + " deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
            + " refs/heads/master\n";
    PGPSignatureGenerator gen = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(signingKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1));
    gen.init(PGPSignature.BINARY_DOCUMENT, signingKey.getPrivateKey());
    gen.update(payload.getBytes(UTF_8));
    PGPSignature sig = gen.generate();

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try (BCPGOutputStream out = new BCPGOutputStream(new ArmoredOutputStream(bout))) {
        sig.encode(out);
    }//from  w  w w  .  j  av  a2 s .co  m

    String cert = payload + new String(bout.toByteArray(), UTF_8);
    Reader reader = new InputStreamReader(new ByteArrayInputStream(cert.getBytes(UTF_8)));
    PushCertificateParser parser = new PushCertificateParser(tr.getRepository(), signedPushConfig);
    return parser.parse(reader);
}

From source file:com.lyndir.lhunath.opal.crypto.gpg.GPG.java

License:Apache License

/**
 * PGP sign a stream./* w  w w .  ja v a2  s . c om*/
 *
 * @param data       The stream that contains the data to sign.
 * @param privateKey The private key to use for signing.
 * @param passPhrase The passphrase that the private key is locked with.
 * @param armoured   {@code true}: ASCII armor the signature.
 *
 * @return The signature.
 *
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 * @throws SignatureException
 * @throws FileNotFoundException
 * @throws PGPException
 * @throws IOException
 */
public static InputStream sign(final InputStream data, final PGPSecretKey privateKey, final String passPhrase,
        final boolean armoured) throws NoSuchAlgorithmException, NoSuchProviderException, PGPException,
        SignatureException, IOException {

    /* Build the signature generator. */
    PGPSignatureGenerator signer = new PGPSignatureGenerator(privateKey.getPublicKey().getAlgorithm(),
            HashAlgorithmTags.SHA1, BouncyCastleProvider.PROVIDER_NAME);
    signer.initSign(PGPSignature.BINARY_DOCUMENT,
            privateKey.extractPrivateKey(passPhrase.toCharArray(), BouncyCastleProvider.PROVIDER_NAME));

    /* Write the data into the generator. */
    byte[] buffer = new byte[4096];
    for (int read; (read = data.read(buffer)) >= 0;)
        signer.update(buffer, 0, read);

    /* Create the signature output stream, armour if necessary. */
    try (ByteArrayOutputStream signatureByteStream = new ByteArrayOutputStream();
            OutputStream signatureStream = armoured ? new ArmoredOutputStream(signatureByteStream)
                    : signatureByteStream) {

        /* Create and write out the signature. */
        PGPSignature signature = signer.generate();
        signature.encode(signatureStream);

        return new ByteArrayInputStream(signatureByteStream.toByteArray());
    }
}

From source file:de.dentrassi.pm.signing.pgp.internal.PgpSigningService.java

License:Open Source License

@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));
    signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, this.privateKey);

    final ArmoredOutputStream armoredOutput = new ArmoredOutputStream(out);

    if (inline) {
        armoredOutput.beginClearText(digest);
    }//from   w w w  . j  a va 2  s. co  m

    final byte[] buffer = new byte[4096];

    int rc;
    while ((rc = in.read(buffer)) >= 0) {
        if (inline) {
            armoredOutput.write(buffer, 0, rc);
        }
        signatureGenerator.update(buffer, 0, rc);
    }

    armoredOutput.endClearText();

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

    armoredOutput.close();
}

From source file:org.eclipse.packagedrone.repo.signing.pgp.internal.AbstractSecretKeySigningService.java

License:Open Source License

@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 {//  www  .ja v a2 s  .c o  m
        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();
}

From source file:org.eclipse.packagedrone.repo.signing.pgp.SigningStream.java

License:Open Source License

@Override
public void close() throws IOException {
    testInit();/*from  ww  w  .  ja  va  2s  .  c  o m*/

    if (this.inline) {
        this.armoredOutput.endClearText();
    }

    try {
        final PGPSignature signature = this.signatureGenerator.generate();
        signature.encode(new BCPGOutputStream(this.armoredOutput));
    } catch (final PGPException e) {
        throw new IOException(e);
    }

    this.armoredOutput.close();

    super.close();
}

From source file:org.gradle.plugins.signing.signatory.pgp.PgpSignatory.java

License:Apache License

private void writeSignatureTo(OutputStream signatureDestination, PGPSignature pgpSignature)
        throws PGPException, IOException {
    // BCPGOutputStream seems to do some internal buffering, it's unclear whether it's stricly required here though
    BCPGOutputStream bufferedOutput = new BCPGOutputStream(signatureDestination);
    pgpSignature.encode(bufferedOutput);
    bufferedOutput.flush();/*from   ww w .  j  a  v a 2s .c  om*/
}

From source file:org.m1theo.apt.repo.signing.PGPSigner.java

License:Apache License

/**
 * Creates a clear sign signature over the input data. (Not detached)
 *
 * @param input      the content to be signed
 * @param output     the output destination of the signature
 *//*from   w  ww .  j  a v a 2s .c o  m*/
public void clearSign(InputStream input, OutputStream output)
        throws IOException, PGPException, GeneralSecurityException {

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), digest));
    signatureGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privateKey);

    ArmoredOutputStream armoredOutput = new ArmoredOutputStream(output);
    armoredOutput.beginClearText(digest);

    LineIterator iterator = new LineIterator(new InputStreamReader(input));

    while (iterator.hasNext()) {
        String line = iterator.nextLine();

        // trailing spaces must be removed for signature calculation (see http://tools.ietf.org/html/rfc4880#section-7.1)
        byte[] data = trim(line).getBytes("UTF-8");

        armoredOutput.write(data);
        armoredOutput.write(EOL);

        signatureGenerator.update(data);
        if (iterator.hasNext()) {
            signatureGenerator.update(EOL);
        }
    }

    armoredOutput.endClearText();

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

    armoredOutput.close();
}

From source file:org.m1theo.apt.repo.signing.PGPSigner.java

License:Apache License

/**
 * Creates a detached clear sign signature over the input data.
 *
 * @param input      the content to be signed
 * @param output     the output destination of the signature
 *//*from  w w w  .  j a  v  a  2s  . co  m*/
public void clearSignDetached(InputStream input, OutputStream output)
        throws IOException, PGPException, GeneralSecurityException {

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), digest));
    signatureGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privateKey);

    ArmoredOutputStream armoredOutput = new ArmoredOutputStream(output);

    LineIterator iterator = new LineIterator(new InputStreamReader(input));

    while (iterator.hasNext()) {
        String line = iterator.nextLine();

        // trailing spaces must be removed for signature calculation (see http://tools.ietf.org/html/rfc4880#section-7.1)
        byte[] data = trim(line).getBytes("UTF-8");

        signatureGenerator.update(data);
        if (iterator.hasNext()) {
            signatureGenerator.update(EOL);
        }
    }

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

    armoredOutput.close();
}

From source file:org.vafer.jdeb.signing.PGPSigner.java

License:Apache License

/**
 * Creates a clear sign signature over the input data. (Not detached)
 *
 * @param input      the content to be signed
 * @param output     the output destination of the signature
 *//*from   www.j  a va2s. c  om*/
public void clearSign(InputStream input, OutputStream output)
        throws IOException, PGPException, GeneralSecurityException {
    int digest = PGPUtil.SHA1;

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), digest));
    signatureGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privateKey);

    ArmoredOutputStream armoredOutput = new ArmoredOutputStream(output);
    armoredOutput.beginClearText(digest);

    LineIterator iterator = new LineIterator(new InputStreamReader(input));

    while (iterator.hasNext()) {
        String line = iterator.nextLine();

        // trailing spaces must be removed for signature calculation (see http://tools.ietf.org/html/rfc4880#section-7.1)
        byte[] data = trim(line).getBytes("UTF-8");

        armoredOutput.write(data);
        armoredOutput.write(EOL);

        signatureGenerator.update(data);
        if (iterator.hasNext()) {
            signatureGenerator.update(EOL);
        }
    }

    armoredOutput.endClearText();

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

    armoredOutput.close();
}