Skip to content

Commit 2af75fa

Browse files
committed
Actually settle on SHA1PRNG if it's available. #1405
I botched the previous patch a bit by unconditionally re-assigning the secureRandom local to a default JDK new SecureRandom. This could cause systems without the default preferred PRNG (NativePRNGNonBlocking, Java 8+) to have slower thread startup and/or random number generation.
1 parent 4e076c1 commit 2af75fa

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

core/src/main/java/org/jruby/runtime/ThreadContext.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,35 @@ public static ThreadContext newContext(Ruby runtime) {
147147
private static boolean tryPreferredPRNG = true;
148148
private static boolean trySHA1PRNG = true;
149149

150+
@SuppressWarnings("deprecation")
150151
public SecureRandom getSecureRandom() {
151152
SecureRandom secureRandom = this.secureRandom;
152-
if (secureRandom == null) {
153-
if (tryPreferredPRNG) {
154-
try {
155-
secureRandom = SecureRandom.getInstance(Options.PREFERRED_PRNG.load());
156-
} catch (Exception e) {
157-
tryPreferredPRNG = false;
158-
}
153+
154+
// Try preferred PRNG, which defaults to NativePRNGNonBlocking
155+
if (secureRandom == null && tryPreferredPRNG) {
156+
try {
157+
secureRandom = SecureRandom.getInstance(Options.PREFERRED_PRNG.load());
158+
} catch (Exception e) {
159+
tryPreferredPRNG = false;
159160
}
160-
if (secureRandom == null) {
161-
if (trySHA1PRNG) {
162-
try {
163-
secureRandom = SecureRandom.getInstance("SHA1PRNG");
164-
} catch (Exception e) {
165-
trySHA1PRNG = false;
166-
}
167-
}
168-
secureRandom = new SecureRandom();
161+
}
162+
163+
// Try SHA1PRNG
164+
if (secureRandom == null && trySHA1PRNG) {
165+
try {
166+
secureRandom = SecureRandom.getInstance("SHA1PRNG");
167+
} catch (Exception e) {
168+
trySHA1PRNG = false;
169169
}
170-
this.secureRandom = secureRandom;
171170
}
171+
172+
// Just let JDK do whatever it does
173+
if (secureRandom == null) {
174+
secureRandom = new SecureRandom();
175+
}
176+
177+
this.secureRandom = secureRandom;
178+
172179
return secureRandom;
173180
}
174181

0 commit comments

Comments
 (0)