Example usage for org.bouncycastle.openpgp PGPSecretKeyRingCollection getKeyRings

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

Introduction

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

Prototype

public Iterator<PGPSecretKeyRing> getKeyRings(String userID) throws PGPException 

Source Link

Document

Return an iterator of the key rings associated with the passed in userID.

Usage

From source file:dorkbox.util.crypto.CryptoPGP.java

License:Apache License

/**
 * Find private gpg key in InputStream, also closes the input stream
 *
 * @param inputStream//from ww  w.  ja  v  a  2 s .c o  m
 *                 the inputStream that contains the private (secret) key
 * @param userId
 *                 the user id
 *
 * @return the PGP secret key
 */
public static List<PGPSecretKey> getSecretKeys(InputStream inputStream, String userId) throws PGPException {
    // iterate over every private key in the key ring
    PGPSecretKeyRingCollection secretKeyRings;
    try {
        secretKeyRings = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(inputStream),
                fingerprintCalculator);
    } catch (IOException e) {
        throw new PGPException("No private key found in stream!", e);
    } finally {
        IO.close(inputStream);
    }

    // look for the key ring that is used to authenticate our reporting facilities
    Iterator<PGPSecretKeyRing> secretKeys = secretKeyRings.getKeyRings(userId);
    List<PGPSecretKey> pgpSecretKeys = new ArrayList<PGPSecretKey>();

    // iterate over every private key in the ring
    while (secretKeys.hasNext()) {
        PGPSecretKeyRing secretKeyRing = secretKeys.next();
        PGPSecretKey tmpKey = secretKeyRing.getSecretKey();

        if (tmpKey != null) {
            pgpSecretKeys.add(tmpKey);
        }
    }

    if (!pgpSecretKeys.isEmpty()) {
        return pgpSecretKeys;
    }

    throw new PGPException("No private key found in stream!");
}