Search in sources :

Example 1 with BCPGOutputStream

use of org.bouncycastle.bcpg.BCPGOutputStream in project camel by apache.

the class PGPDataFormatTest method createSignature.

private void createSignature(OutputStream out) throws Exception {
    PGPSecretKey pgpSec = readSecretKey();
    PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(getProvider()).build("sdude".toCharArray()));
    PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(getProvider()));
    sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
    BCPGOutputStream bOut = new BCPGOutputStream(out);
    InputStream fIn = new ByteArrayInputStream("Test Signature".getBytes("UTF-8"));
    int ch;
    while ((ch = fIn.read()) >= 0) {
        sGen.update((byte) ch);
    }
    fIn.close();
    sGen.generate().encode(bOut);
}
Also used : PGPSignatureGenerator(org.bouncycastle.openpgp.PGPSignatureGenerator) JcaPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) JcePBESecretKeyDecryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)

Example 2 with BCPGOutputStream

use of org.bouncycastle.bcpg.BCPGOutputStream in project gradle by gradle.

the class PgpSignatory method writeSignatureTo.

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();
}
Also used : BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream)

Example 3 with BCPGOutputStream

use of org.bouncycastle.bcpg.BCPGOutputStream in project gerrit by GerritCodeReview.

the class PushCertificateCheckerTest method newSignedCert.

private PushCertificate newSignedCert(String nonce, TestKey signingKey, Date now) 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));
    if (now != null) {
        PGPSignatureSubpacketGenerator subGen = new PGPSignatureSubpacketGenerator();
        subGen.setSignatureCreationTime(false, now);
        gen.setHashedSubpackets(subGen.generate());
    }
    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);
    }
    String cert = payload + new String(bout.toByteArray(), UTF_8);
    Reader reader = new InputStreamReader(new ByteArrayInputStream(cert.getBytes(UTF_8)));
    PushCertificateParser parser = new PushCertificateParser(repo, signedPushConfig);
    return parser.parse(reader);
}
Also used : PGPSignatureGenerator(org.bouncycastle.openpgp.PGPSignatureGenerator) PushCertificateParser(org.eclipse.jgit.transport.PushCertificateParser) InputStreamReader(java.io.InputStreamReader) BcPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) PGPSignature(org.bouncycastle.openpgp.PGPSignature) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PushCertificateIdent(org.eclipse.jgit.transport.PushCertificateIdent) ByteArrayInputStream(java.io.ByteArrayInputStream) PGPSignatureSubpacketGenerator(org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator)

Aggregations

BCPGOutputStream (org.bouncycastle.bcpg.BCPGOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 PGPSignatureGenerator (org.bouncycastle.openpgp.PGPSignatureGenerator)2 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)1 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 ArmoredOutputStream (org.bouncycastle.bcpg.ArmoredOutputStream)1 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)1 PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)1 PGPSignature (org.bouncycastle.openpgp.PGPSignature)1 PGPSignatureSubpacketGenerator (org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator)1 BcPGPContentSignerBuilder (org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder)1 JcaPGPContentSignerBuilder (org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder)1 JcePBESecretKeyDecryptorBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)1 PushCertificateIdent (org.eclipse.jgit.transport.PushCertificateIdent)1 PushCertificateParser (org.eclipse.jgit.transport.PushCertificateParser)1