From 66b02f839439d77a1a588aa72bad3683e5bdf200 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 27 Sep 2025 14:48:56 +0200 Subject: [PATCH] OpenPGPKeyReader: Use consistent method name parseKeyOrCertificate() Added new methods as replacement for mis-named 'parseCertificateOrKey()' methods and deprecated old methods --- .../openpgp/api/OpenPGPKeyReader.java | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyReader.java b/pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyReader.java index de20829c78..28545a192d 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyReader.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyReader.java @@ -49,7 +49,7 @@ public OpenPGPKeyReader(OpenPGPImplementation implementation, OpenPGPPolicy poli public OpenPGPCertificate parseCertificate(String armored) throws IOException { - OpenPGPCertificate certificate = parseCertificateOrKey(armored); + OpenPGPCertificate certificate = parseKeyOrCertificate(armored); if (certificate instanceof OpenPGPKey) { throw new IOException("Could not parse OpenPGPCertificate: Is OpenPGPKey."); @@ -67,7 +67,7 @@ public OpenPGPCertificate parseCertificate(String armored) public OpenPGPCertificate parseCertificate(InputStream inputStream) throws IOException { - OpenPGPCertificate certificate = parseCertificateOrKey(inputStream); + OpenPGPCertificate certificate = parseKeyOrCertificate(inputStream); if (certificate instanceof OpenPGPKey) { throw new IOException("Could not parse OpenPGPCertificate: Is OpenPGPKey."); @@ -85,7 +85,7 @@ public OpenPGPCertificate parseCertificate(InputStream inputStream) public OpenPGPCertificate parseCertificate(byte[] bytes) throws IOException { - OpenPGPCertificate certificate = parseCertificateOrKey(bytes); + OpenPGPCertificate certificate = parseKeyOrCertificate(bytes); if (certificate instanceof OpenPGPKey) { throw new IOException("Could not parse OpenPGPCertificate: Is OpenPGPKey."); @@ -99,11 +99,14 @@ public OpenPGPCertificate parseCertificate(byte[] bytes) * @param armored ASCII armored string * @return parsed certificate or key * @throws IOException if the key or certificate cannot be parsed + * + * @deprecated use {@link #parseKeyOrCertificate(String)} instead. */ + @Deprecated public OpenPGPCertificate parseCertificateOrKey(String armored) throws IOException { - return parseCertificateOrKey(Strings.toUTF8ByteArray(armored)); + return parseKeyOrCertificate(armored); } /** @@ -112,11 +115,14 @@ public OpenPGPCertificate parseCertificateOrKey(String armored) * @param inputStream input stream containing the ASCII armored or binary key or certificate * @return parsed certificate or key * @throws IOException if the key or certificate cannot be parsed + * + * @deprecated use {@link #parseKeyOrCertificate(InputStream)} instead. */ + @Deprecated public OpenPGPCertificate parseCertificateOrKey(InputStream inputStream) throws IOException { - return parseCertificateOrKey(Streams.readAll(inputStream)); + return parseKeyOrCertificate(inputStream); } /** @@ -125,9 +131,51 @@ public OpenPGPCertificate parseCertificateOrKey(InputStream inputStream) * @param bytes ASCII armored or binary key or certificate * @return parsed certificate or key * @throws IOException if the key or certificate cannot be parsed + * + * @deprecated use {@link #parseKeyOrCertificate(byte[])} instead. */ + @Deprecated public OpenPGPCertificate parseCertificateOrKey(byte[] bytes) throws IOException + { + return parseKeyOrCertificate(bytes); + } + + /** + * Parse a single {@link OpenPGPCertificate} or {@link OpenPGPKey} from an ASCII armored string. + * + * @param armored ASCII armored string + * @return parsed certificate or key + * @throws IOException if the key or certificate cannot be parsed + */ + public OpenPGPCertificate parseKeyOrCertificate(String armored) + throws IOException + { + return parseKeyOrCertificate(Strings.toUTF8ByteArray(armored)); + } + + /** + * Parse a single {@link OpenPGPCertificate} or {@link OpenPGPKey} from an {@link InputStream}. + * + * @param inputStream input stream containing the ASCII armored or binary key or certificate + * @return parsed certificate or key + * @throws IOException if the key or certificate cannot be parsed + */ + public OpenPGPCertificate parseKeyOrCertificate(InputStream inputStream) + throws IOException + { + return parseKeyOrCertificate(Streams.readAll(inputStream)); + } + + /** + * Parse a single {@link OpenPGPCertificate} or {@link OpenPGPKey} from bytes. + * + * @param bytes ASCII armored or binary key or certificate + * @return parsed certificate or key + * @throws IOException if the key or certificate cannot be parsed + */ + public OpenPGPCertificate parseKeyOrCertificate(byte[] bytes) + throws IOException { ByteArrayInputStream bIn = new ByteArrayInputStream(bytes); InputStream decoderStream = PGPUtil.getDecoderStream(bIn);