/** Always returns a {@link InMemoryKeyring} instance. */ @Provides static Keyring provideKeyring() { PGPKeyPair dummyKey; try (InputStream publicInput = PGP_PUBLIC_KEYRING.openStream(); InputStream privateInput = PGP_PRIVATE_KEYRING.openStream()) { PGPPublicKeyRingCollection publicKeys = new BcPGPPublicKeyRingCollection(PGPUtil.getDecoderStream(publicInput)); PGPSecretKeyRingCollection privateKeys = new BcPGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privateInput)); dummyKey = lookupKeyPair(publicKeys, privateKeys, EMAIL_ADDRESS, ENCRYPT_SIGN); } catch (PGPException | IOException e) { throw new VerifyException("Failed to load PGP keys from jar", e); } // Use the same dummy PGP keypair for all required PGP keys -- a real production system would // have different values for these keys. Pass dummy values for all Strings. return new InMemoryKeyring( dummyKey, dummyKey, dummyKey.getPublicKey(), dummyKey, dummyKey.getPublicKey(), "not a real key", "not a real key", "not a real password", "not a real login", "not a real password", "not a real login", "not a real credential", "not a real key"); }
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; }
public static Stream<PGPKeyRing> streamKeyring ( final InputStream input ) throws IOException, PGPException { final BcPGPSecretKeyRingCollection keyrings = new BcPGPSecretKeyRingCollection ( PGPUtil.getDecoderStream ( input ) ); final Iterator<?> keyRingIter = keyrings.getKeyRings (); final Stream<?> s = StreamSupport.stream ( Spliterators.spliteratorUnknownSize ( keyRingIter, Spliterator.ORDERED ), false ); return s.map ( o -> (PGPKeyRing)o ); }