Example usage for org.bouncycastle.openpgp.operator.bc BcPGPDigestCalculatorProvider BcPGPDigestCalculatorProvider

List of usage examples for org.bouncycastle.openpgp.operator.bc BcPGPDigestCalculatorProvider BcPGPDigestCalculatorProvider

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp.operator.bc BcPGPDigestCalculatorProvider BcPGPDigestCalculatorProvider.

Prototype

BcPGPDigestCalculatorProvider

Source Link

Usage

From source file:alpha.offsync.security.OpenPGPSecurityUtility.java

License:Apache License

@Override
public void sign(final OutputStream outputStream, final InputStream inputStream, final String keyInfo) {
    try {/*www  .  ja  v a 2  s  .  c  o  m*/
        final File keyFile = this.secretKeyRing;
        final char[] pass = this.secretKeyRingPassword;

        final ArmoredOutputStream out = new ArmoredOutputStream(outputStream);

        final PGPSecretKey pgpSec = this.getSignKey(keyInfo); // readSecretKey(new
        // FileInputStream(keyFile));
        final PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(
                new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));
        final PGPSignatureGenerator sGen = new PGPSignatureGenerator(
                new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1));

        sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

        final Iterator it = pgpSec.getPublicKey().getUserIDs();
        if (it.hasNext()) {
            final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();

            spGen.setSignerUserID(false, (String) it.next());
            sGen.setHashedSubpackets(spGen.generate());
        }

        final PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB);

        final BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out));

        sGen.generateOnePassVersion(false).encode(bOut);

        final PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
        final byte[] buffer = new byte[1 << 16];
        final OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, "", new Date(), buffer);
        int ch = 0;

        while ((ch = inputStream.read()) >= 0) {
            lOut.write(ch);
            sGen.update((byte) ch);
        }

        lGen.close();

        sGen.generate().encode(bOut);
        cGen.close();

        out.close();
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final PGPException e) {
        e.printStackTrace();
    } catch (final SignatureException e) {
        e.printStackTrace();
    }
}

From source file:alpha.offsync.security.OpenPGPSecurityUtility.java

License:Apache License

/**
 * Finds the secret key of a {@link PGPSecretKeyRingCollection}.
 * /* ww  w  .  j a v  a 2  s .  c om*/
 * @param pgpSec
 *            the {@link PGPSecretKeyRingCollection}
 * @param keyID
 *            the key id
 * @param pass
 *            the secret key password
 * @return the {@link PGPPrivateKey}
 * @throws PGPException
 *             thrown if an error is encountered
 */
private PGPPrivateKey findSecretKey(final PGPSecretKeyRingCollection pgpSec, final long keyID,
        final char[] pass) throws PGPException {
    final PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);

    if (pgpSecKey == null)
        return null;

    return pgpSecKey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
            .build(this.secretKeyRingPassword));
}

From source file:com.arcusx.simplepgp.PgpKeyUtils.java

public static PGPPrivateKey getPrivateKeyFrom(PGPSecretKey secretKey) throws PGPException, IOException {
    PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
            .build("".toCharArray());
    return secretKey.extractPrivateKey(decryptor);
}

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

License:Apache License

public PGPSigner(InputStream keyring, String keyId, String passphrase) throws IOException, PGPException {
    secretKey = getSecretKey(keyring, keyId);
    if (secretKey == null) {
        throw new PGPException(String.format("Specified key %s does not exist in key ring %s", keyId, keyring));
    }//w ww.  j  av a2 s . c o m
    privateKey = secretKey
            .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
                    .build(passphrase.toCharArray()));
}

From source file:com.google.e2e.bcdriver.Decryptor.java

License:Apache License

static final PGPPrivateKey extractDecryptionKey(PGPSecretKeyRing pskr, String pass) throws PGPException {
    Iterator<PGPSecretKey> skit = Util.getTypedIterator(pskr.getSecretKeys(), PGPSecretKey.class);

    PGPSecretKey selected = null;/* w  w w. j a  v a2  s.  co m*/

    // Pass #1 - use key flags on signatures.
    while (skit.hasNext()) {
        PGPSecretKey sk = skit.next();
        Iterator<PGPSignature> sigit = Util.getTypedIterator(sk.getPublicKey().getSignatures(),
                PGPSignature.class);
        while (sigit.hasNext()) {
            if (Util.hasKeyFlag(sigit.next(), KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) {
                selected = sk;
                break;
            }
        }
    }
    if (selected == null) {
        // Pass #2 - use intrinsic key capabilities, but prefer subkeys
        // where possible.
        skit = Util.getTypedIterator(pskr.getSecretKeys(), PGPSecretKey.class);
        while (skit.hasNext()) {
            PGPSecretKey sk = skit.next();
            if (sk.getPublicKey().isEncryptionKey()) {
                selected = sk;
                // But continue the loop, so subkeys will be chosen.
            }
        }
    }

    if (selected != null) {
        return selected
                .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
                        .build(pass.toCharArray()));
    } else {
        return null;
    }
}

From source file:com.google.gerrit.gpg.testutil.TestKey.java

License:Apache License

public PGPPrivateKey getPrivateKey() throws PGPException {
    return getSecretKey()
            .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
                    // All test keys have no passphrase.
                    .build(new char[0]));
}

From source file:com.google.gerrit.server.git.gpg.TestKey.java

License:Apache License

PGPPrivateKey getPrivateKey() throws PGPException {
    return sec.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
            // All test keys have no passphrase.
            .build(new char[0]));
}

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

License:Open Source License

public PgpSigningService(final InputStream keyring, final String keyId, final String passphrase)
        throws IOException, PGPException {
    this.secretKey = PgpHelper.loadSecretKey(keyring, keyId);
    if (this.secretKey == null) {
        throw new IllegalStateException(String.format("Signing key '%08X' could not be found", keyId));
    }/*from   www  . j  a  v a 2s. c o  m*/
    this.privateKey = this.secretKey
            .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
                    .build(passphrase.toCharArray()));
}

From source file:de.dentrassi.pm.signing.pgp.web.ServiceController.java

License:Open Source License

@ControllerValidator(formDataClass = AddEntry.class)
public void validateAdd(final AddEntry data, final ValidationContext context) {
    final String keyring = data.getKeyring();
    final File file = new File(keyring);

    if (!file.exists()) {
        context.error("keyring",
                String.format("File '%s' does not exist on the server", file.getAbsolutePath()));
        return;//from w ww.  j av a2 s.c  o m
    }
    if (!file.isFile()) {
        context.error("keyring", String.format("File '%s' is not a file", file.getAbsolutePath()));
        return;
    }
    if (!file.canRead()) {
        context.error("keyring", String.format("File '%s' cannot be read", file.getAbsolutePath()));
        return;
    }

    final String keyId = data.getKeyId();
    if (keyId != null) {
        try {
            try (InputStream input = new FileInputStream(file)) {
                final PGPSecretKey key = PgpHelper.loadSecretKey(input, keyId);
                if (key == null) {
                    context.error("keyId", "Key not found in keyring");
                } else if (data.getKeyPassphrase() != null) {
                    try {
                        final PGPPrivateKey privateKey = key.extractPrivateKey(
                                new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
                                        .build(data.getKeyPassphrase().toCharArray()));
                        if (privateKey == null) {
                            Thread.sleep(1_000);
                            context.error("keyPassphrase", "Unable to unlock private key");
                        }
                    } catch (final Exception e) {
                        context.error("Failed to load key. Probably a wrong phassphrase: "
                                + ExceptionHelper.getMessage(e));
                    }
                }
            }
        } catch (final Exception e) {
            context.error("Failed to load key: " + ExceptionHelper.getMessage(e));
        }
    }
}

From source file:google.registry.keyring.api.KeySerializer.java

License:Open Source License

private static PBESecretKeyDecryptor createSecretKeyDecryptor() {
    // There shouldn't be a passphrase on the key
    return new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(new char[0]);
}