Java Code Examples for org.bouncycastle.openpgp.PGPSecretKey#getKeyID()

The following examples show how to use org.bouncycastle.openpgp.PGPSecretKey#getKeyID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PgpHelper.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
public static Predicate<PGPSecretKey> keyShortId ( final String keyId )
{
    final long keyIdNum = Long.parseUnsignedLong ( keyId, 16 );

    return new Predicate<PGPSecretKey> () {

        @Override
        public boolean test ( final PGPSecretKey key )
        {
            final long shortId = key.getKeyID () & 0xFFFFFFFFL;

            if ( key.getKeyID () != keyIdNum && shortId != keyIdNum )
            {
                return false;
            }

            return true;
        }
    };
}
 
Example 2
Source File: EncryptionServicePgpImpl.java    From pgptool with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private PGPPublicKeyEncryptedData getPublicKeyEncryptedDataByKeyId(InputStream in, PGPSecretKey secretKey) {
	try {
		PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(in),
				KeyFilesOperationsPgpImpl.fingerprintCalculator);

		for (Iterator iter = factory.iterator(); iter.hasNext();) {
			Object section = iter.next();
			if (section instanceof PGPEncryptedDataList) {
				PGPEncryptedDataList d = (PGPEncryptedDataList) section;
				for (Iterator dataIter = d.getEncryptedDataObjects(); dataIter.hasNext();) {
					PGPPublicKeyEncryptedData data = (PGPPublicKeyEncryptedData) dataIter.next();
					if (data.getKeyID() == secretKey.getKeyID()) {
						return data;
					}
				}
			}
		}
		// NOTE: That is actually should NEVER happen since secret key we're
		// supposed to use here was taken exactly same way as we're looking
		// for PGPPublicKeyEncryptedData now
		throw new RuntimeException("Encryption data matching given key "
				+ KeyDataPgp.buildKeyIdStr(secretKey.getKeyID()) + " wasn't found");
	} catch (Throwable t) {
		throw new RuntimeException("Failed to find Encryption data section in encrypted file", t);
	}
}
 
Example 3
Source File: PgpHelper.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
public static PGPSecretKey loadSecretKey ( final InputStream input, final String keyId ) throws IOException, PGPException
{
    final long keyIdNum = Long.parseUnsignedLong ( keyId, 16 );

    final BcPGPSecretKeyRingCollection keyrings = new BcPGPSecretKeyRingCollection ( PGPUtil.getDecoderStream ( input ) );

    final Iterator<?> keyRingIter = keyrings.getKeyRings ();
    while ( keyRingIter.hasNext () )
    {
        final PGPSecretKeyRing secretKeyRing = (PGPSecretKeyRing)keyRingIter.next ();

        final Iterator<?> secretKeyIterator = secretKeyRing.getSecretKeys ();
        while ( secretKeyIterator.hasNext () )
        {
            final PGPSecretKey key = (PGPSecretKey)secretKeyIterator.next ();

            if ( !key.isSigningKey () )
            {
                continue;
            }

            final long shortId = key.getKeyID () & 0xFFFFFFFFL;

            if ( key.getKeyID () != keyIdNum && shortId != keyIdNum )
            {
                continue;
            }

            return key;
        }
    }

    return null;
}
 
Example 4
Source File: OpenPGPSignatureGenerator.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
    in = PGPUtil.getDecoderStream(in);
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in,
            new BcKeyFingerprintCalculator());

    PGPSecretKey key = null;
    Iterator<PGPSecretKeyRing> it = pgpSec.getKeyRings();
    while (key == null && it.hasNext()) {
        PGPSecretKeyRing kRing = it.next();

        Iterator<PGPSecretKey> it2 = kRing.getSecretKeys();
        while (key == null && it2.hasNext()) {
            PGPSecretKey k = it2.next();
            if (keyId == null && k.isSigningKey()) {
                key = k;
            }
            if (keyId != null && Long.valueOf(keyId, 16) == (k.getKeyID() & MASK)) {
                key = k;
            }
        }
    }

    if (key == null) {
        throw new IllegalArgumentException("Can't find encryption key"
                + (keyId != null ? " '" + keyId + "' " : " ") + "in key ring.");
    }

    return key;
}
 
Example 5
Source File: PgpHelper.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
public static String makeShortKey ( final PGPSecretKey key )
{
    final long shortId = key.getKeyID () & 0xFFFFFFFFL;
    return String.format ( "%08X", shortId );
}