Skip to content

Commit 4377f00

Browse files
committed
[GR-69655] Simplify PythonContext.getSecureRandom().
PullRequest: graalpython/4002
2 parents 6bcf6a6 + f2fbb7a commit 4377f00

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import java.nio.file.LinkOption;
7676
import java.security.NoSuchAlgorithmException;
7777
import java.security.SecureRandom;
78+
import java.security.Security;
7879
import java.text.MessageFormat;
7980
import java.util.ArrayDeque;
8081
import java.util.ArrayList;
@@ -1510,18 +1511,14 @@ public SecureRandom getSecureRandom() {
15101511
assert !env.isPreInitialization();
15111512
if (secureRandom == null) {
15121513
CompilerDirectives.transferToInterpreterAndInvalidate();
1513-
try {
1514-
secureRandom = SecureRandom.getInstance("NativePRNGNonBlocking");
1515-
} catch (NoSuchAlgorithmException e) {
1516-
if (getPythonOS() == PLATFORM_WIN32) {
1517-
try {
1518-
secureRandom = SecureRandom.getInstanceStrong();
1519-
} catch (NoSuchAlgorithmException e2) {
1520-
throw new RuntimeException("Unable to obtain entropy source for random number generation (NativePRNGNonBlocking)", e2);
1521-
}
1522-
} else {
1523-
throw new RuntimeException("Unable to obtain entropy source for random number generation (NativePRNGNonBlocking)", e);
1514+
if (Security.getAlgorithms("SecureRandom").contains("NATIVEPRNGNONBLOCKING")) {
1515+
try {
1516+
secureRandom = SecureRandom.getInstance("NATIVEPRNGNONBLOCKING");
1517+
} catch (NoSuchAlgorithmException e) {
1518+
throw CompilerDirectives.shouldNotReachHere(e);
15241519
}
1520+
} else {
1521+
secureRandom = new SecureRandom();
15251522
}
15261523
}
15271524
return secureRandom;

0 commit comments

Comments
 (0)