Skip to content

Commit e002e72

Browse files
committed
instantiate SecureRandom only once
1 parent 1498df9 commit e002e72

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/crypto/impl/AesCryptoSupport.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.ByteBuffer;
2323
import java.security.GeneralSecurityException;
2424
import java.security.Key;
25+
import java.security.NoSuchAlgorithmException;
2526
import java.security.SecureRandom;
2627
import java.util.Random;
2728

@@ -30,6 +31,7 @@
3031
import javax.crypto.spec.IvParameterSpec;
3132
import javax.crypto.spec.SecretKeySpec;
3233

34+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3335
import io.wcm.devops.conga.plugins.aem.crypto.CryptoSupport;
3436

3537
/**
@@ -45,13 +47,15 @@ public class AesCryptoSupport extends CryptoSupport {
4547

4648
private static final String SECURE_RANDOM_ALGORITHM = "SHA1PRNG";
4749

48-
private final Random random;
50+
private static final Random RANDOM = initRandom();
4951

50-
/**
51-
* @throws GeneralSecurityException Security exception
52-
*/
53-
public AesCryptoSupport() throws GeneralSecurityException {
54-
this.random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM);
52+
private static Random initRandom() {
53+
try {
54+
return SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM);
55+
}
56+
catch (NoSuchAlgorithmException ex) {
57+
throw new RuntimeException(ex);
58+
}
5559
}
5660

5761
@Override
@@ -91,9 +95,10 @@ private Cipher getCipher(int cipherMode, byte[] iv, Key key) throws GeneralSecur
9195
return cipher;
9296
}
9397

98+
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE")
9499
private byte[] generateIV() {
95100
byte[] iv = new byte[IV_SIZE];
96-
random.nextBytes(iv);
101+
RANDOM.nextBytes(iv);
97102
return iv;
98103
}
99104

0 commit comments

Comments
 (0)