PgpHelper { @Nullable @CheckResult @VisibleForTesting static String reformatPgpPublicKeyBlock(@NonNull String text) { StringBuilder reformattedKeyBlocks = new StringBuilder(); while (!text.isEmpty()) { int indexOfBlock = text.indexOf("-----BEGIN PGP PUBLIC KEY BLOCK-----"); int indexOfBlockEnd = text.indexOf("-----END PGP PUBLIC KEY BLOCK-----"); if (indexOfBlock < 0 || indexOfBlockEnd < 0) { break; } Matcher matcher = KEYDATA_START_PATTERN.matcher(text); if (!matcher.find()) { Timber.e("Could not find start of key data!"); break; } int indexOfPubkeyMaterial = matcher.start(1); String keyMaterial = text.substring(indexOfPubkeyMaterial, indexOfBlockEnd); keyMaterial = keyMaterial.replaceAll("\\s+", "\n"); reformattedKeyBlocks.append("-----BEGIN PGP PUBLIC KEY BLOCK-----\n"); reformattedKeyBlocks.append('\n'); reformattedKeyBlocks.append(keyMaterial); reformattedKeyBlocks.append("-----END PGP PUBLIC KEY BLOCK-----\n"); text = text.substring(indexOfBlockEnd +34).trim(); } if (reformattedKeyBlocks.length() == 0) { return null; } return reformattedKeyBlocks.toString(); }  static String fixPgpMessage(String message); static String fixPgpCleartextSignature(CharSequence input); static String getPgpMessageContent(@NonNull CharSequence input); static String getPgpPublicKeyContent(@NonNull CharSequence input); @Nullable @CheckResult @VisibleForTesting /* Reformats a key block with messed up whitespace. This will strip headers in the process. */ static String reformatPgpPublicKeyBlock(@NonNull String text); static final Pattern PGP_MESSAGE; static final Pattern PGP_CLEARTEXT_SIGNATURE; static final Pattern PGP_PUBLIC_KEY; }
@Test public void reformatPgpPublicKeyBlock() throws Exception { String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK_TWO_OCTET_LENGTH); assertNotNull(reformattedKey); UncachedKeyRing.decodeFromData(reformattedKey.getBytes()); }
@Test public void reformatPgpPublicKeyBlock_consecutiveKeys() throws Exception { String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock( INPUT_KEY_BLOCK_TWO_OCTET_LENGTH + INPUT_KEY_BLOCK_TWO_OCTET_LENGTH); assertNotNull(reformattedKey); IteratorWithIOThrow<UncachedKeyRing> uncachedKeyRingIteratorWithIOThrow = UncachedKeyRing.fromStream(new ByteArrayInputStream(reformattedKey.getBytes())); assertNotNull(uncachedKeyRingIteratorWithIOThrow.next()); assertNotNull(uncachedKeyRingIteratorWithIOThrow.next()); assertFalse(uncachedKeyRingIteratorWithIOThrow.hasNext()); }
@Test public void reformatPgpPublicKeyBlock_shouldBeIdempotent() throws Exception { String reformattedKey1 = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK_TWO_OCTET_LENGTH); assertNotNull(reformattedKey1); String reformattedKey2 = PgpHelper.reformatPgpPublicKeyBlock(reformattedKey1); assertEquals(reformattedKey1, reformattedKey2); }
@Test public void reformatPgpPublicKeyBlock_withOneOctetLengthHeader() throws Exception { String reformattedKey = PgpHelper.reformatPgpPublicKeyBlock(INPUT_KEY_BLOCK_ONE_OCTET_LENGTH); assertNotNull(reformattedKey); UncachedKeyRing.decodeFromData(reformattedKey.getBytes()); }