Example usage for org.bouncycastle.openpgp PGPSecretKeyRingCollection size

List of usage examples for org.bouncycastle.openpgp PGPSecretKeyRingCollection size

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPSecretKeyRingCollection size.

Prototype

public int size() 

Source Link

Document

Return the number of rings in this collection.

Usage

From source file:org.apache.camel.converter.crypto.PGPDataFormatUtil.java

License:Apache License

@Deprecated
private static PGPPrivateKey findPrivateKey(InputStream keyringInput, InputStream encryptedInput,
        String passphrase, PGPPassphraseAccessor passphraseAccessor, String provider)
        throws IOException, PGPException, NoSuchProviderException {
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyringInput));
    PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(encryptedInput));
    PGPEncryptedDataList enc;//w  w  w . j a v  a 2  s . c o m
    Object o = factory.nextObject();
    if (o == null) {
        throw new PGPException("Provided input is not encrypted.");
    }
    if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;
    } else {
        enc = (PGPEncryptedDataList) factory.nextObject();
    }
    encryptedInput.reset(); // nextObject() method reads from the InputStream, so rewind it!
    Iterator<?> encryptedDataObjects = enc.getEncryptedDataObjects();
    PGPPrivateKey privateKey = null;
    PGPPublicKeyEncryptedData encryptedData = null;
    while (privateKey == null && encryptedDataObjects.hasNext()) {
        encryptedData = (PGPPublicKeyEncryptedData) encryptedDataObjects.next();
        PGPSecretKey pgpSecKey = pgpSec.getSecretKey(encryptedData.getKeyID());
        if (pgpSecKey != null) {
            if (passphrase == null && passphraseAccessor != null) {
                // get passphrase from accessor
                @SuppressWarnings("unchecked")
                Iterator<String> userIDs = pgpSecKey.getUserIDs();
                while (passphrase == null && userIDs.hasNext()) {
                    passphrase = passphraseAccessor.getPassphrase(userIDs.next());
                }
            }
            privateKey = pgpSecKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(provider)
                    .build(passphrase.toCharArray()));
        }
    }
    if (privateKey == null && pgpSec.size() > 0 && encryptedData != null) {
        throw new PGPException("Provided input is encrypted with unknown pair of keys.");
    }
    return privateKey;
}