Skip to content

Commit 21c0a58

Browse files
committed
Java 1.5 to 1.8 back-porting.
1 parent 843a99a commit 21c0a58

36 files changed

+849
-438
lines changed

core/src/test/java/org/bouncycastle/crypto/test/AESTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bouncycastle.crypto.test;
22

3-
import java.nio.charset.StandardCharsets;
43
import java.security.SecureRandom;
54
import java.util.Random;
65

@@ -18,6 +17,7 @@
1817
import org.bouncycastle.crypto.params.KeyParameter;
1918
import org.bouncycastle.crypto.params.ParametersWithIV;
2019
import org.bouncycastle.util.Arrays;
20+
import org.bouncycastle.util.Strings;
2121
import org.bouncycastle.util.encoders.Hex;
2222
import org.bouncycastle.util.test.SimpleTest;
2323

@@ -563,15 +563,15 @@ private void testCounter() {
563563

564564
private void verify(String inStr) {
565565
SICBlockCipher cipher = newCipher();
566-
byte[] bytes = inStr.getBytes(StandardCharsets.UTF_8);
566+
byte[] bytes = Strings.toUTF8ByteArray(inStr);
567567

568568
appendFile(bytes, cipher);
569569
appendFile(bytes, cipher);
570570
appendFile(bytes, cipher);
571571

572572
byte[] out = new byte[fileBytes.length];
573573
newCipher().processBytes(fileBytes, 0, fileBytes.length, out, 0);
574-
String outStr = new String(out, StandardCharsets.UTF_8);
574+
String outStr = Strings.fromUTF8ByteArray(out);
575575

576576
if (!outStr.equals(inStr + inStr + inStr)) {
577577
throw new RuntimeException("fail");

core/src/test/java/org/bouncycastle/crypto/test/CipherTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.bouncycastle.crypto.test;
22

33
import java.security.SecureRandom;
4-
import java.util.Arrays;
54

65
import org.bouncycastle.crypto.BlockCipher;
76
import org.bouncycastle.crypto.CipherKeyGenerator;
@@ -11,6 +10,7 @@
1110
import org.bouncycastle.crypto.modes.AEADCipher;
1211
import org.bouncycastle.crypto.params.KeyParameter;
1312
import org.bouncycastle.crypto.params.ParametersWithIV;
13+
import org.bouncycastle.util.Arrays;
1414
import org.bouncycastle.util.test.SimpleTest;
1515

1616
public abstract class CipherTest
@@ -175,7 +175,7 @@ static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
175175
final byte[] myResult = Arrays.copyOf(myDecrypted, msgLen);
176176

177177
/* Check that we have the same result */
178-
if (!Arrays.equals(myData, myResult))
178+
if (!Arrays.areEqual(myData, myResult))
179179
{
180180
System.out.println("Cipher " + pCipher.getAlgorithmName() + " failed");
181181
}

pg/src/main/java/org/bouncycastle/bcpg/sig/PreferredAEADCiphersuites.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.bouncycastle.bcpg.sig;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import org.bouncycastle.bcpg.AEADAlgorithmTags;
47
import org.bouncycastle.bcpg.SignatureSubpacketTags;
58
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
69

7-
import java.util.ArrayList;
8-
import java.util.List;
9-
1010
/**
1111
* Signature Subpacket containing the AEAD cipher suites (AEAD algorithm, Symmetric Key Algorithm pairs)
1212
* preferred by the key holder's implementation.
@@ -169,7 +169,7 @@ public static Builder builder(boolean isCritical)
169169
public static final class Builder
170170
{
171171

172-
private final List<Combination> combinations = new ArrayList<>();
172+
private final List<Combination> combinations = new ArrayList<Combination>();
173173
private final boolean isCritical;
174174

175175
private Builder(boolean isCritical)

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public Iterator<PGPSignature> getSignaturesForKeyID(
569569

570570
public Iterator<PGPSignature> getSignaturesForKey(KeyIdentifier identifier)
571571
{
572-
List<PGPSignature> sigs = new ArrayList<>();
572+
List<PGPSignature> sigs = new ArrayList<PGPSignature>();
573573
for (Iterator<PGPSignature> it = getSignatures(); it.hasNext(); )
574574
{
575575
PGPSignature sig = it.next();

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKeyRing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public PGPPublicKey getPublicKey(KeyIdentifier identifier)
211211
@Override
212212
public Iterator<PGPPublicKey> getPublicKeys(KeyIdentifier identifier)
213213
{
214-
List<PGPPublicKey> matches = new ArrayList<>();
214+
List<PGPPublicKey> matches = new ArrayList<PGPPublicKey>();
215215
for (PGPPublicKey k : keys)
216216
{
217217
if (identifier.matches(k.getKeyIdentifier()))
@@ -250,7 +250,7 @@ public Iterator<PGPPublicKey> getKeysWithSignaturesBy(long keyID)
250250
@Override
251251
public Iterator<PGPPublicKey> getKeysWithSignaturesBy(KeyIdentifier identifier)
252252
{
253-
List<PGPPublicKey> keysWithSigs = new ArrayList<>();
253+
List<PGPPublicKey> keysWithSigs = new ArrayList<PGPPublicKey>();
254254
for (PGPPublicKey k : keys)
255255
{
256256
Iterator<PGPSignature> sigIt = k.getSignaturesForKey(identifier);

pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public PGPPublicKey getPublicKey(KeyIdentifier identifier)
281281
@Override
282282
public Iterator<PGPPublicKey> getPublicKeys(KeyIdentifier identifier)
283283
{
284-
List<PGPPublicKey> matches = new ArrayList<>();
284+
List<PGPPublicKey> matches = new ArrayList<PGPPublicKey>();
285285
for (PGPSecretKey k : keys)
286286
{
287287
if (k.getPublicKey() != null && identifier.matches(k.getKeyIdentifier()))
@@ -314,7 +314,7 @@ public PGPSecretKey getSecretKey(KeyIdentifier identifier)
314314

315315
public Iterator<PGPSecretKey> getSecretKeys(KeyIdentifier identifier)
316316
{
317-
List<PGPSecretKey> matches = new ArrayList<>();
317+
List<PGPSecretKey> matches = new ArrayList<PGPSecretKey>();
318318
for (PGPSecretKey k : keys)
319319
{
320320
if (identifier.matches(k.getKeyIdentifier()))
@@ -353,7 +353,7 @@ public Iterator<PGPPublicKey> getKeysWithSignaturesBy(long keyID)
353353
@Override
354354
public Iterator<PGPPublicKey> getKeysWithSignaturesBy(KeyIdentifier identifier)
355355
{
356-
List<PGPPublicKey> keysWithSigs = new ArrayList<>();
356+
List<PGPPublicKey> keysWithSigs = new ArrayList<PGPPublicKey>();
357357
for (PGPSecretKey k : keys)
358358
{
359359
if (k.getPublicKey() == null)

pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public long getKeyID()
632632
*/
633633
public List<KeyIdentifier> getKeyIdentifiers()
634634
{
635-
List<KeyIdentifier> identifiers = new ArrayList<>();
635+
List<KeyIdentifier> identifiers = new ArrayList<KeyIdentifier>();
636636
identifiers.addAll(getHashedKeyIdentifiers());
637637
identifiers.addAll(getUnhashedKeyIdentifiers());
638638
return identifiers;
@@ -664,7 +664,7 @@ public List<KeyIdentifier> getUnhashedKeyIdentifiers()
664664

665665
private List<KeyIdentifier> extractKeyIdentifiers(SignatureSubpacket[] subpackets)
666666
{
667-
List<KeyIdentifier> identifiers = new ArrayList<>();
667+
List<KeyIdentifier> identifiers = new ArrayList<KeyIdentifier>();
668668
for (SignatureSubpacket s : subpackets)
669669
{
670670
if (s instanceof IssuerFingerprint)

pg/src/main/java/org/bouncycastle/openpgp/operator/bc/BcAEADSecretKeyEncryptorBuilder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.bouncycastle.openpgp.operator.bc;
22

3+
import java.io.IOException;
4+
import java.security.SecureRandom;
5+
36
import org.bouncycastle.bcpg.AEADUtils;
47
import org.bouncycastle.bcpg.PacketTags;
58
import org.bouncycastle.bcpg.PublicKeyPacket;
@@ -16,9 +19,6 @@
1619
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
1720
import org.bouncycastle.util.Arrays;
1821

19-
import java.io.IOException;
20-
import java.security.SecureRandom;
21-
2222
public class BcAEADSecretKeyEncryptorBuilder
2323
{
2424

@@ -33,7 +33,7 @@ public BcAEADSecretKeyEncryptorBuilder(int aeadAlgorithm, int symmetricAlgorithm
3333
this.argon2Params = argon2Params;
3434
}
3535

36-
public PBESecretKeyEncryptor build(char[] passphrase, PublicKeyPacket pubKey)
36+
public PBESecretKeyEncryptor build(char[] passphrase, final PublicKeyPacket pubKey)
3737
{
3838
return new PBESecretKeyEncryptor(symmetricAlgorithm, aeadAlgorithm, argon2Params, new SecureRandom(), passphrase)
3939
{
@@ -83,7 +83,11 @@ public byte[] encryptKeyData(byte[] key, byte[] keyData, int keyOff, int keyLen)
8383
cipher.doFinal(encKey, dataLen);
8484
return encKey;
8585
}
86-
catch (IOException | InvalidCipherTextException e)
86+
catch (IOException e)
87+
{
88+
throw new PGPException("Exception AEAD protecting private key material", e);
89+
}
90+
catch (InvalidCipherTextException e)
8791
{
8892
throw new PGPException("Exception AEAD protecting private key material", e);
8993
}

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaAEADSecretKeyEncryptorBuilder.java

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

3+
import java.security.Provider;
4+
import java.security.SecureRandom;
5+
6+
import javax.crypto.Cipher;
7+
import javax.crypto.SecretKey;
8+
import javax.crypto.spec.SecretKeySpec;
9+
310
import org.bouncycastle.bcpg.AEADUtils;
411
import org.bouncycastle.bcpg.PacketTags;
512
import org.bouncycastle.bcpg.PublicKeyPacket;
@@ -16,17 +23,6 @@
1623
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
1724
import org.bouncycastle.util.Arrays;
1825

19-
import javax.crypto.BadPaddingException;
20-
import javax.crypto.Cipher;
21-
import javax.crypto.IllegalBlockSizeException;
22-
import javax.crypto.SecretKey;
23-
import javax.crypto.spec.SecretKeySpec;
24-
import java.io.IOException;
25-
import java.security.InvalidAlgorithmParameterException;
26-
import java.security.InvalidKeyException;
27-
import java.security.Provider;
28-
import java.security.SecureRandom;
29-
3026
public class JcaAEADSecretKeyEncryptorBuilder
3127
{
3228
private int aeadAlgorithm;
@@ -59,7 +55,7 @@ public JcaAEADSecretKeyEncryptorBuilder setProvider(String providerName)
5955
return this;
6056
}
6157

62-
public PBESecretKeyEncryptor build(char[] passphrase, PublicKeyPacket pubKey)
58+
public PBESecretKeyEncryptor build(char[] passphrase, final PublicKeyPacket pubKey)
6359
{
6460
return new PBESecretKeyEncryptor(symmetricAlgorithm, aeadAlgorithm, argon2Params, new SecureRandom(), passphrase)
6561
{
@@ -102,8 +98,7 @@ public byte[] encryptKeyData(byte[] key, byte[] keyData, int keyOff, int keyLen)
10298
byte[] data = c.doFinal(keyData, keyOff, keyLen);
10399
return data;
104100
}
105-
catch (IOException | InvalidAlgorithmParameterException | InvalidKeyException |
106-
IllegalBlockSizeException | BadPaddingException e)
101+
catch (Exception e)
107102
{
108103
throw new PGPException("Exception AEAD protecting private key material", e);
109104
}

pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcaKeyFingerprintCalculator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.openpgp.operator.jcajce;
22

33
import java.io.IOException;
4+
import java.security.GeneralSecurityException;
45
import java.security.MessageDigest;
56
import java.security.NoSuchAlgorithmException;
67
import java.security.NoSuchProviderException;
@@ -79,7 +80,7 @@ public byte[] calculateFingerprint(PublicKeyPacket publicPk)
7980

8081
return digest.digest();
8182
}
82-
catch (NoSuchAlgorithmException | NoSuchProviderException e)
83+
catch (GeneralSecurityException e)
8384
{
8485
throw new PGPException("can't find MD5", e);
8586
}
@@ -103,7 +104,7 @@ else if (publicPk.getVersion() == PublicKeyPacket.VERSION_4)
103104

104105
return digest.digest();
105106
}
106-
catch (NoSuchAlgorithmException | NoSuchProviderException e)
107+
catch (GeneralSecurityException e)
107108
{
108109
throw new PGPException("can't find SHA1", e);
109110
}

0 commit comments

Comments
 (0)