Skip to content

Commit 60afb39

Browse files
committed
Java 5 to Java 8 compatibility fixes.
1 parent 8c9500f commit 60afb39

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyReader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.ByteArrayInputStream;
44
import java.io.IOException;
55
import java.io.InputStream;
6-
import java.nio.charset.StandardCharsets;
76
import java.util.ArrayList;
87
import java.util.List;
98

@@ -13,6 +12,7 @@
1312
import org.bouncycastle.openpgp.PGPPublicKeyRing;
1413
import org.bouncycastle.openpgp.PGPSecretKeyRing;
1514
import org.bouncycastle.openpgp.PGPUtil;
15+
import org.bouncycastle.util.Strings;
1616
import org.bouncycastle.util.io.Streams;
1717

1818
/**
@@ -103,7 +103,7 @@ public OpenPGPCertificate parseCertificate(byte[] bytes)
103103
public OpenPGPCertificate parseCertificateOrKey(String armored)
104104
throws IOException
105105
{
106-
return parseCertificateOrKey(armored.getBytes(StandardCharsets.UTF_8));
106+
return parseCertificateOrKey(Strings.toUTF8ByteArray(armored));
107107
}
108108

109109
/**
@@ -163,7 +163,7 @@ else if (object instanceof PGPPublicKeyRing)
163163
public OpenPGPKey parseKey(String armored)
164164
throws IOException
165165
{
166-
return parseKey(armored.getBytes(StandardCharsets.UTF_8));
166+
return parseKey(Strings.toUTF8ByteArray(armored));
167167
}
168168

169169
/**
@@ -211,7 +211,7 @@ public OpenPGPKey parseKey(byte[] bytes)
211211
public List<OpenPGPCertificate> parseKeysOrCertificates(String armored)
212212
throws IOException
213213
{
214-
return parseKeysOrCertificates(armored.getBytes(StandardCharsets.UTF_8));
214+
return parseKeysOrCertificates(Strings.toUTF8ByteArray(armored));
215215
}
216216

217217
public List<OpenPGPCertificate> parseKeysOrCertificates(InputStream inputStream)
@@ -259,7 +259,7 @@ else if (object instanceof PGPPublicKeyRing)
259259
public List<OpenPGPCertificate> parseCertificates(String armored)
260260
throws IOException
261261
{
262-
return parseCertificates(armored.getBytes(StandardCharsets.UTF_8));
262+
return parseCertificates(Strings.toUTF8ByteArray(armored));
263263
}
264264

265265
public List<OpenPGPCertificate> parseCertificates(InputStream inputStream)
@@ -303,7 +303,7 @@ else if (object instanceof PGPPublicKeyRing)
303303
public List<OpenPGPKey> parseKeys(String armored)
304304
throws IOException
305305
{
306-
return parseKeys(armored.getBytes(StandardCharsets.UTF_8));
306+
return parseKeys(Strings.toUTF8ByteArray(armored));
307307
}
308308

309309
public List<OpenPGPKey> parseKeys(InputStream inputStream)

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPSignature.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.bouncycastle.openpgp.api;
22

3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.util.Date;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.Locale;
9+
310
import org.bouncycastle.bcpg.ArmoredOutputStream;
411
import org.bouncycastle.bcpg.BCPGOutputStream;
512
import org.bouncycastle.bcpg.KeyIdentifier;
@@ -17,12 +24,6 @@
1724
import org.bouncycastle.openpgp.api.util.UTCUtil;
1825
import org.bouncycastle.util.encoders.Hex;
1926

20-
import java.io.ByteArrayOutputStream;
21-
import java.io.IOException;
22-
import java.util.Date;
23-
import java.util.List;
24-
import java.util.Locale;
25-
2627
/**
2728
* An OpenPGP signature.
2829
* This is a wrapper around {@link PGPSignature} which tracks the verification state of the signature.
@@ -125,8 +126,10 @@ public static KeyIdentifier getMostExpressiveIdentifier(List<KeyIdentifier> iden
125126
}
126127

127128
// Find most expressive identifier
128-
for (KeyIdentifier identifier : identifiers)
129+
for (Iterator it = identifiers.iterator(); it.hasNext();)
129130
{
131+
KeyIdentifier identifier = (KeyIdentifier)it.next();
132+
130133
// non-wildcard and has fingerprint
131134
if (!identifier.isWildcard() && identifier.getFingerprint() != null)
132135
{
@@ -135,8 +138,9 @@ public static KeyIdentifier getMostExpressiveIdentifier(List<KeyIdentifier> iden
135138
}
136139

137140
// Find non-wildcard identifier
138-
for (KeyIdentifier identifier : identifiers)
141+
for (Iterator it = identifiers.iterator(); it.hasNext();)
139142
{
143+
KeyIdentifier identifier = (KeyIdentifier)it.next();
140144
// non-wildcard (and no fingerprint)
141145
if (!identifier.isWildcard())
142146
{
@@ -295,17 +299,20 @@ void sanitize(OpenPGPCertificate.OpenPGPComponentKey issuer,
295299
this, "Signature predates issuer key creation time.");
296300
}
297301

298-
for (NotationData notation : hashed.getNotationDataOccurrences())
302+
for (Iterator it = hashed.getNotationDataOccurrences().iterator(); it.hasNext(); )
299303
{
304+
NotationData notation = (NotationData)it.next();
300305
if (notation.isCritical())
301306
{
302307
throw new MalformedOpenPGPSignatureException(
303-
this, "Critical unknown NotationData encountered: " + notation.getNotationName());
308+
this, "Critical unknown NotationData encountered: " + notation.getNotationName());
304309
}
305310
}
306311

307-
for (SignatureSubpacket unknownSubpacket : hashed.toArray())
312+
SignatureSubpacket[] signatureSubpackets = hashed.toArray();
313+
for (int i = 0; i != signatureSubpackets.length; i++)
308314
{
315+
SignatureSubpacket unknownSubpacket = signatureSubpackets[i];
309316
// SignatureSubpacketInputStream returns unknown subpackets as SignatureSubpacket
310317
if (unknownSubpacket.isCritical() &&
311318
unknownSubpacket.getClass().equals(SignatureSubpacket.class))

pg/src/test/java/org/bouncycastle/openpgp/api/test/OpenPGPMessageProcessorTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.io.OutputStream;
8-
import java.nio.charset.StandardCharsets;
98
import java.util.List;
109

1110
import org.bouncycastle.bcpg.AEADAlgorithmTags;
@@ -30,12 +29,13 @@
3029
import org.bouncycastle.openpgp.api.OpenPGPPolicy;
3130
import org.bouncycastle.openpgp.api.OpenPGPSignature;
3231
import org.bouncycastle.util.Arrays;
32+
import org.bouncycastle.util.Strings;
3333
import org.bouncycastle.util.io.Streams;
3434

3535
public class OpenPGPMessageProcessorTest
3636
extends APITest
3737
{
38-
private static final byte[] PLAINTEXT = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
38+
private static final byte[] PLAINTEXT = Strings.toUTF8ByteArray("Hello, World!\n");
3939

4040
private PGPSessionKey encryptionSessionKey;
4141

@@ -83,7 +83,6 @@ private void roundtripUnarmoredPlaintextMessage(OpenPGPApi api)
8383
.setAllowPadding(false)
8484
.setCompressionNegotiator(new OpenPGPMessageGenerator.CompressionNegotiator()
8585
{
86-
@Override
8786
public int negotiateCompression(OpenPGPMessageGenerator messageGenerator, OpenPGPPolicy policy)
8887
{
8988
return CompressionAlgorithmTags.UNCOMPRESSED;
@@ -114,7 +113,6 @@ private void roundtripArmoredPlaintextMessage(OpenPGPApi api)
114113
.setAllowPadding(false)
115114
.setCompressionNegotiator(new OpenPGPMessageGenerator.CompressionNegotiator()
116115
{
117-
@Override
118116
public int negotiateCompression(OpenPGPMessageGenerator messageGenerator, OpenPGPPolicy policy)
119117
{
120118
return CompressionAlgorithmTags.UNCOMPRESSED;
@@ -146,7 +144,6 @@ private void roundTripCompressedMessage(OpenPGPApi api)
146144
.setAllowPadding(false)
147145
.setCompressionNegotiator(new OpenPGPMessageGenerator.CompressionNegotiator()
148146
{
149-
@Override
150147
public int negotiateCompression(OpenPGPMessageGenerator messageGenerator, OpenPGPPolicy policy)
151148
{
152149
return CompressionAlgorithmTags.ZIP;
@@ -192,7 +189,6 @@ public MessageEncryptionMechanism negotiateEncryption(OpenPGPMessageGenerator co
192189
})
193190
.setCompressionNegotiator(new OpenPGPMessageGenerator.CompressionNegotiator()
194191
{
195-
@Override
196192
public int negotiateCompression(OpenPGPMessageGenerator messageGenerator, OpenPGPPolicy policy)
197193
{
198194
return CompressionAlgorithmTags.ZIP;
@@ -464,7 +460,6 @@ private void encryptDecryptWithLockedKey(OpenPGPApi api)
464460
.addDecryptionKey(key)
465461
.setMissingOpenPGPKeyPassphraseProvider(new KeyPassphraseProvider()
466462
{
467-
@Override
468463
public char[] getKeyPassword(OpenPGPKey.OpenPGPSecretKey key)
469464
{
470465
return OpenPGPTestKeys.V6_KEY_LOCKED_PASSPHRASE.toCharArray();
@@ -500,7 +495,6 @@ private void encryptDecryptWithMissingKey(OpenPGPApi api)
500495
OpenPGPMessageInputStream decIn = api.decryptAndOrVerifyMessage()
501496
.setMissingOpenPGPKeyProvider(new OpenPGPKeyMaterialProvider.OpenPGPKeyProvider()
502497
{
503-
@Override
504498
public OpenPGPKey provide(KeyIdentifier componentKeyIdentifier)
505499
{
506500
return key;
@@ -650,7 +644,7 @@ private void incompleteMessageProcessing(OpenPGPApi api)
650644
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
651645
OpenPGPMessageOutputStream out = gen.open(bOut);
652646

653-
out.write("Some Data".getBytes(StandardCharsets.UTF_8));
647+
out.write(Strings.toUTF8ByteArray("Some Data"));
654648
out.close();
655649

656650
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
@@ -688,7 +682,7 @@ private void testVerificationOfSEIPD1MessageWithTamperedCiphertext(OpenPGPApi ap
688682
OpenPGPKey key = api.readKeyOrCertificate().parseKey(OpenPGPTestKeys.BOB_KEY);
689683
OpenPGPMessageProcessor processor = api.decryptAndOrVerifyMessage();
690684
processor.addDecryptionKey(key);
691-
OpenPGPMessageInputStream oIn = processor.process(new ByteArrayInputStream(MSG.getBytes(StandardCharsets.UTF_8)));
685+
OpenPGPMessageInputStream oIn = processor.process(new ByteArrayInputStream(Strings.toUTF8ByteArray(MSG)));
692686
Streams.drain(oIn);
693687
try
694688
{

pkix/src/test/java/org/bouncycastle/cert/test/GOSTR3410_2012_256GenerateCertificate.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.security.Provider;
1010
import java.security.Security;
1111
import java.security.spec.ECGenParameterSpec;
12-
import java.time.ZonedDateTime;
1312
import java.util.Date;
1413

1514
import org.bouncycastle.asn1.ASN1Sequence;
@@ -60,14 +59,14 @@ private static X509CertificateHolder generateSelfSignedCertificate()
6059
X500Name subject = new X500Name("CN=TEST");
6160
X500Name issuer = subject;
6261
BigInteger serial = BigInteger.ONE;
63-
ZonedDateTime notBefore = ZonedDateTime.now();
64-
ZonedDateTime notAfter = notBefore.plusYears(1);
62+
Date notBefore = new Date();
63+
Date notAfter = new Date(notBefore.getTime() + 1000L * 60 * 60 * 24 * 365);
6564

6665
X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(
6766
issuer,
6867
serial,
69-
Date.from(notBefore.toInstant()),
70-
Date.from(notAfter.toInstant()),
68+
notBefore,
69+
notAfter,
7170
subject,
7271
keyPair.getPublic()
7372
);

prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/FalconTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.bouncycastle.pqc.jcajce.spec.FalconParameterSpec;
3131
import org.bouncycastle.util.Arrays;
3232
import org.bouncycastle.util.Strings;
33+
import org.bouncycastle.util.encoders.Base64;
3334
import org.bouncycastle.util.encoders.Hex;
3435
import org.bouncycastle.util.io.pem.PemObject;
3536
import org.bouncycastle.util.io.pem.PemReader;
@@ -101,7 +102,7 @@ public void testOQSPublicKeyExample()
101102
System.out.println("Public Key Algorithm : " + publicKey.getAlgorithm());
102103
System.out.println("Public Key Format : " + publicKey.getFormat());
103104
System.out.println("Encoded Key Length : " + publicKey.getEncoded().length + " bytes");
104-
System.out.println("Encoded Key (Base64) : " + java.util.Base64.getEncoder().encodeToString(publicKey.getEncoded()));
105+
System.out.println("Encoded Key (Base64) : " + Base64.toBase64String(publicKey.getEncoded()));
105106

106107
}
107108
catch (Exception e)

0 commit comments

Comments
 (0)