Example usage for org.bouncycastle.openpgp PGPPublicKeyRingCollection size

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Return the number of rings in this collection.

Usage

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

License:Open Source License

public PGPPublicKeyRingCollection readPublicKeyRing(String baseDir)
        throws FileNotFoundException, IOException, PGPException {
    PGPPublicKeyRingCollection pubRings = null;
    PGPPublicKeyRing pgpPub = null;/*from   w  w  w . ja  v a  2 s  . co m*/

    // directory that contains all the .asc files
    File dir = new File(baseDir + "/KeyRings/Public");

    // list all the files
    String[] children = dir.list();
    if (children == null) {
        // Either dir does not exist or is not a directory
    } else {
        for (int i = 0; i < children.length; i++) {
            String filename = children[i];
            log.info("File Name (.asc) " + "(" + i + ")" + " = " + filename);
            PGPPublicKeyRingCollection tmpKeyRingCollection = readPublicKeyRingCollection(
                    new File(dir, filename));

            if (pubRings == null) {
                // read the first .asc file and create the
                // PGPPublicKeyRingCollection to hold all the other key
                // rings
                pubRings = tmpKeyRingCollection;
            } else {
                PGPPublicKeyRingCollection otherKeyRings = tmpKeyRingCollection;

                @SuppressWarnings("unchecked")
                Iterator<PGPPublicKeyRing> rIt = otherKeyRings.getKeyRings();
                while (rIt.hasNext()) {
                    pgpPub = rIt.next();
                }

                //TODO bobby doesn't this belong inside the loop?
                // copy the key ring to PGPPublicKeyCollection pubRings
                pubRings = PGPPublicKeyRingCollection.addPublicKeyRing(pubRings, pgpPub);
            }
        } // end of for

        // size should equal the number of the .asc files
        log.debug("Collection size = " + pubRings.size());
    } // end of else

    return pubRings;
}

From source file:com.google.gerrit.gpg.PublicKeyStore.java

License:Apache License

private void saveToNotes(ObjectInserter ins, PGPPublicKeyRing keyRing) throws PGPException, IOException {
    long keyId = keyRing.getPublicKey().getKeyID();
    PGPPublicKeyRingCollection existing = get(keyId);
    List<PGPPublicKeyRing> toWrite = new ArrayList<>(existing.size() + 1);
    boolean replaced = false;
    for (PGPPublicKeyRing kr : existing) {
        if (sameKey(keyRing, kr)) {
            toWrite.add(keyRing);/*from   w w  w  .  ja  va2s . co  m*/
            replaced = true;
        } else {
            toWrite.add(kr);
        }
    }
    if (!replaced) {
        toWrite.add(keyRing);
    }
    notes.set(keyObjectId(keyId), ins.insert(OBJ_BLOB, keysToArmored(toWrite)));
}

From source file:com.google.gerrit.gpg.PublicKeyStore.java

License:Apache License

private void deleteFromNotes(ObjectInserter ins, Fingerprint fp) throws PGPException, IOException {
    long keyId = fp.getId();
    PGPPublicKeyRingCollection existing = get(keyId);
    List<PGPPublicKeyRing> toWrite = new ArrayList<>(existing.size());
    for (PGPPublicKeyRing kr : existing) {
        if (!fp.equalsBytes(kr.getPublicKey().getFingerprint())) {
            toWrite.add(kr);/*from  ww w  .  jav  a 2  s  .  c o m*/
        }
    }
    if (toWrite.size() == existing.size()) {
        return;
    } else if (!toWrite.isEmpty()) {
        notes.set(keyObjectId(keyId), ins.insert(OBJ_BLOB, keysToArmored(toWrite)));
    } else {
        notes.remove(keyObjectId(keyId));
    }
}

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

License:Apache License

/**
 * Find public gpg key in InputStream./*from   ww  w. j av  a 2 s  .c o m*/
 *
 * @param inputStream
 *                 the input stream
 *
 * @return the PGP public key
 */
private static PGPPublicKey findPublicGPGKey(InputStream inputStream) throws IOException, PGPException {

    // get all key rings in the input stream
    PGPPublicKeyRingCollection publicKeyRingCollection = new PGPPublicKeyRingCollection(
            PGPUtil.getDecoderStream(inputStream), fingerprintCalculator);

    System.err.println("key ring size: " + publicKeyRingCollection.size());

    Iterator<PGPPublicKeyRing> keyRingIter = publicKeyRingCollection.getKeyRings();

    // iterate over keyrings
    while (keyRingIter.hasNext()) {
        PGPPublicKeyRing keyRing = keyRingIter.next();
        Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys();
        // iterate over public keys in the key ring
        while (keyIter.hasNext()) {
            PGPPublicKey tmpKey = keyIter.next();

            if (tmpKey == null) {
                break;
            }

            Iterator<String> userIDs = tmpKey.getUserIDs();
            ArrayList<String> strings = new ArrayList<String>();
            while (userIDs.hasNext()) {
                String next = userIDs.next();
                strings.add(next);
            }

            System.err.println("Encryption key = " + tmpKey.isEncryptionKey() + ", Master key = "
                    + tmpKey.isMasterKey() + ", UserId = " + strings);

            // we need a master encryption key
            if (tmpKey.isEncryptionKey() && tmpKey.isMasterKey()) {
                return tmpKey;
            }
        }
    }
    throw new PGPException("No public key found!");
}