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() 

Source Link

Document

return the secret key rings making up this collection.

Usage

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

License:Apache License

/**
 * Gets the correct signing key from local secret keyring using the supplied
 * key information.// w w  w.  j ava2  s . c  o  m
 * 
 * @param keyInfo
 *            the supplied key information
 * @return the correct signing key
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws PGPException
 *             thrown if an error is encountered
 */
public PGPSecretKey getSignKey(final String keyInfo) throws IOException, PGPException {
    final PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new FileInputStream(this.secretKeyRing)));

    final Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext()) {
        final PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

        final Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext()) {
            final PGPSecretKey key = (PGPSecretKey) keyIter.next();

            final Iterator idIter = key.getUserIDs();
            while (idIter.hasNext()) {
                final String userID = idIter.next().toString();
                if (userID.contains(keyInfo) && key.isSigningKey())
                    return key;
            }

        }
    }

    return null;
}

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

public static PGPSecretKey findSecretKey(InputStream privateKeyIn) throws PGPException, IOException {

    //TODO FIXME/* w  ww .  j a  v a2 s.c om*/
    InputStream in = PGPUtil.getDecoderStream(privateKeyIn);
    KeyFingerPrintCalculator fingerPrintCalculator = null;

    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in, fingerPrintCalculator);

    for (Iterator<PGPSecretKeyRing> iter = pgpSec.getKeyRings(); iter.hasNext();) {
        PGPSecretKeyRing pgpSecretKeyRing = iter.next();
        for (Iterator<PGPSecretKey> keysIter = pgpSecretKeyRing.getSecretKeys(); keysIter.hasNext();) {
            PGPSecretKey secretKey = keysIter.next();
            return secretKey;
        }
    }

    throw new NoSuchElementException("No private key found.");
}

From source file:com.geekcommune.identity.EncryptionUtil.java

License:Open Source License

/**
 * A simple routine that opens a key ring file and finds the first available
 * key suitable for signature generation.
 * /*from  w w  w  .j  a  v a2s  .  co m*/
 * @param in
 * @return
 * @throws IOException
 * @throws PGPException
 */
private static PGPSecretKey findSigningKey(PGPSecretKeyRingCollection secRing)
        throws IOException, PGPException {
    //
    // We just loop through the collection till we find a key suitable for encryption.
    //
    PGPSecretKey key = null;

    @SuppressWarnings("unchecked")
    Iterator<PGPSecretKeyRing> rIt = secRing.getKeyRings();

    while (key == null && rIt.hasNext()) {
        PGPSecretKeyRing kRing = rIt.next();
        @SuppressWarnings("unchecked")
        Iterator<PGPSecretKey> kIt = kRing.getSecretKeys();

        while (key == null && kIt.hasNext()) {
            PGPSecretKey k = (PGPSecretKey) kIt.next();
            if (k.isSigningKey()) {
                key = k;
            }
        }
    }

    if (key == null) {
        throw new PGPException("Can't find a signing key in the key ring");
    }

    return key;
}

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

License:Apache License

/**
 * Returns the secret key matching the specified identifier.
 * /*ww w .j av  a 2  s. c om*/
 * @param input the input stream containing the keyring collection
 * @param keyId the 4 bytes identifier of the key
 */
private PGPSecretKey getSecretKey(InputStream input, String keyId) throws IOException, PGPException {
    PGPSecretKeyRingCollection keyrings = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));

    Iterator rIt = keyrings.getKeyRings();

    while (rIt.hasNext()) {
        PGPSecretKeyRing kRing = (PGPSecretKeyRing) rIt.next();
        Iterator kIt = kRing.getSecretKeys();

        while (kIt.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) kIt.next();

            if (key.isSigningKey()
                    && Long.toHexString(key.getKeyID() & 0xFFFFFFFFL).equals(keyId.toLowerCase())) {
                return key;
            }
        }
    }

    return null;
}

From source file:com.lyndir.lhunath.opal.crypto.gpg.GPG.java

License:Apache License

/**
 * @param privateKeyFile The file that contains the private keys.
 *
 * @return all master key IDs available in the given key ring.
 *
 * @throws FileNotFoundException//from w  w w.  j  a v  a  2 s  .  c o m
 * @throws IOException
 * @throws PGPException
 */
public static List<PrintableKeyWrapper<PGPSecretKey>> getPrivateKeys(final File privateKeyFile)
        throws IOException, PGPException {

    /* Open the key ring. */
    try (FileInputStream privateKeyInputStream = new FileInputStream(privateKeyFile)) {
        List<PrintableKeyWrapper<PGPSecretKey>> keys = new ArrayList<>();
        PGPSecretKeyRingCollection privateKeyRing = new PGPSecretKeyRingCollection(
                PGPUtil.getDecoderStream(privateKeyInputStream));

        /* Enumerate the IDs. */
        @SuppressWarnings("unchecked")
        Iterator<PGPSecretKeyRing> rings = privateKeyRing.getKeyRings();
        while (rings.hasNext()) {
            @SuppressWarnings("unchecked")
            Iterator<PGPSecretKey> ring = rings.next().getSecretKeys();
            while (ring.hasNext()) {
                PGPSecretKey key = ring.next();
                if (!key.getUserIDs().hasNext())
                    continue;

                keys.add(new PrintableKeyWrapper<PGPSecretKey>(key, key.getKeyID()) {

                    @Override
                    public String toString() {

                        return getKey().getUserIDs().next().toString();
                    }
                });
            }
        }

        return keys;
    }
}

From source file:com.navnorth.learningregistry.LRSigner.java

License:Apache License

/**
 * Reads private key from the provided InputStream
 *
 * @param input InputStream of the private key
 * @return PGPSecretKey//  ww w  .  j  a v a2  s. c o m
 * @throws LRException NO_KEY error if the key cannot be obtained from the input stream
 */
private PGPSecretKey readSecretKey(InputStream input) throws LRException {
    PGPSecretKeyRingCollection pgpSec;

    try {
        pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
    } catch (Exception e) {
        throw new LRException(LRException.NO_KEY);
    }

    java.util.Iterator keyRingIter = pgpSec.getKeyRings();
    while (keyRingIter.hasNext()) {
        PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

        java.util.Iterator keyIter = keyRing.getSecretKeys();
        while (keyIter.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) keyIter.next();

            if (key.isSigningKey()) {
                return key;
            }
        }
    }

    throw new LRException(LRException.NO_KEY);
}

From source file:com.verhas.licensor.License.java

License:Open Source License

/**
 * Load the secret key to be used to encrypt the license. After the key is
 * loaded it can be used to encrypt license files.
 * //w w w. j  a  v  a2  s  . c  o m
 * @param in
 *            input stream of the file containing the key rings
 * @param userId
 *            the user id of the key. If this parameter is {@code null} then
 *            the first key on the key ring appropriate to sign will be
 *            used.
 * @throws java.io.IOException
 * @throws org.bouncycastle.openpgp.PGPException
 */
@SuppressWarnings("unchecked")
public License loadKey(InputStream in, final String userId) throws IOException, PGPException {
    in = PGPUtil.getDecoderStream(in);

    final PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in);
    key = null;
    for (final PGPSecretKeyRing kRing : in((Iterator<PGPSecretKeyRing>) pgpSec.getKeyRings())) {
        for (final PGPSecretKey k : in((Iterator<PGPSecretKey>) kRing.getSecretKeys())) {
            for (final String keyUserId : in((Iterator<String>) k.getUserIDs())) {
                if (keyIsAppropriate(userId, keyUserId, k)) {
                    key = k;
                    return this;
                }
            }
        }
    }

    throw new IllegalArgumentException("Can't find signing key in key ring.");
}

From source file:com.zwitserloot.ivyplusplus.mavencentral.CreateDetachedSignatures_.java

License:Open Source License

PGPSecretKey getSigningKey(InputStream keyData, String streamName)
        throws IOException, PGPException, SigningException {
    PGPSecretKeyRingCollection keyrings_ = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyData));
    Iterator<?> keyrings = keyrings_.getKeyRings();
    while (keyrings.hasNext()) {
        PGPSecretKeyRing keys_ = (PGPSecretKeyRing) keyrings.next();
        Iterator<?> keys = keys_.getSecretKeys();
        while (keys.hasNext()) {
            PGPSecretKey key = (PGPSecretKey) keys.next();
            if (key.isSigningKey())
                return key;
        }//from  ww w.j  av  a2s  . c o  m
    }

    throw new SigningException("No signing key found in keyring: " + streamName);
}

From source file:crypttools.PGPCryptoBC.java

License:Open Source License

/**
* <p>Return the first suitable key for encryption in the key ring
* collection. For this case we only expect there to be one key
* available for signing.</p>//from w w w  .  ja v a 2  s .  co m
* 
* @param input - the input stream of the key PGP Key Ring
* @return the first suitable PGP Secret Key found for signing
* @throws IOException
* @throws PGPException
*/
@SuppressWarnings("unchecked")
private static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException {
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
    Iterator<PGPSecretKeyRing> iter = pgpSec.getKeyRings();
    PGPSecretKey secKey = null;

    while (iter.hasNext() && secKey == null) {
        PGPSecretKeyRing keyRing = iter.next();
        Iterator<PGPSecretKey> keyIter = keyRing.getSecretKeys();

        while (keyIter.hasNext()) {
            PGPSecretKey key = keyIter.next();
            if (key.isSigningKey()) {
                secKey = key;
                break;
            }
        }
    }

    if (secKey != null) {
        return secKey;
    } else {
        throw new IllegalArgumentException("Can't find signing key in key ring.");
    }
}

From source file:de.softwareforge.pgpsigner.key.SecretKeyRing.java

License:Apache License

public void load(final String ringFileName) throws IOException, PGPException {
    File ringFile = new File(ringFileName);

    if (!ringFile.exists() || !ringFile.isFile()) {
        throw new IOException("Ring file " + ringFileName + " is not a file!");
    }/*  www .ja v  a  2  s .c  om*/

    clear();
    setRingFileName(ringFileName);

    PGPSecretKeyRingCollection secretRing = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(new FileInputStream(ringFile)));

    for (Iterator ringIt = secretRing.getKeyRings(); ringIt.hasNext();) {
        PGPSecretKeyRing keyRing = (PGPSecretKeyRing) ringIt.next();

        for (Iterator it = keyRing.getSecretKeys(); it.hasNext();) {
            PGPSecretKey secretKey = (PGPSecretKey) it.next();

            if (secretKey.isMasterKey()) {
                SecretKey secKey = new SecretKey(secretKey);
                keys.put(secKey.getKeyId(), secKey);
            }
        }
    }
}