Search in sources :

Example 1 with JcaPGPContentSignerBuilder

use of org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder in project camel by apache.

the class PGPKeyAccessDataFormat method createSignatureGenerator.

protected List<PGPSignatureGenerator> createSignatureGenerator(Exchange exchange, OutputStream out) throws Exception {
    if (secretKeyAccessor == null) {
        return null;
    }
    List<String> sigKeyUserids = determineSignaturenUserIds(exchange);
    List<PGPSecretKeyAndPrivateKeyAndUserId> sigSecretKeysWithPrivateKeyAndUserId = secretKeyAccessor.getSignerKeys(exchange, sigKeyUserids);
    if (sigSecretKeysWithPrivateKeyAndUserId.isEmpty()) {
        return null;
    }
    exchange.getOut().setHeader(NUMBER_OF_SIGNING_KEYS, Integer.valueOf(sigSecretKeysWithPrivateKeyAndUserId.size()));
    List<PGPSignatureGenerator> sigGens = new ArrayList<PGPSignatureGenerator>();
    for (PGPSecretKeyAndPrivateKeyAndUserId sigSecretKeyWithPrivateKeyAndUserId : sigSecretKeysWithPrivateKeyAndUserId) {
        PGPPrivateKey sigPrivateKey = sigSecretKeyWithPrivateKeyAndUserId.getPrivateKey();
        PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
        spGen.setSignerUserID(false, sigSecretKeyWithPrivateKeyAndUserId.getUserId());
        int algorithm = sigSecretKeyWithPrivateKeyAndUserId.getSecretKey().getPublicKey().getAlgorithm();
        PGPSignatureGenerator sigGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(algorithm, findHashAlgorithm(exchange)).setProvider(getProvider()));
        sigGen.init(PGPSignature.BINARY_DOCUMENT, sigPrivateKey);
        sigGen.setHashedSubpackets(spGen.generate());
        sigGen.generateOnePassVersion(false).encode(out);
        sigGens.add(sigGen);
    }
    return sigGens;
}
Also used : PGPSignatureGenerator(org.bouncycastle.openpgp.PGPSignatureGenerator) JcaPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder) ArrayList(java.util.ArrayList) PGPSignatureSubpacketGenerator(org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey)

Example 2 with JcaPGPContentSignerBuilder

use of org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder 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)

Aggregations

PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)2 PGPSignatureGenerator (org.bouncycastle.openpgp.PGPSignatureGenerator)2 JcaPGPContentSignerBuilder (org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 BCPGOutputStream (org.bouncycastle.bcpg.BCPGOutputStream)1 PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)1 PGPSignatureSubpacketGenerator (org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator)1 JcePBESecretKeyDecryptorBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)1