|
53 | 53 | import java.io.InputStreamReader;
|
54 | 54 | import java.io.OutputStream;
|
55 | 55 | import java.lang.ProcessBuilder.Redirect;
|
56 |
| -import java.math.BigInteger; |
57 | 56 | import java.net.InetAddress;
|
58 | 57 | import java.net.UnknownHostException;
|
59 | 58 | import java.nio.ByteBuffer;
|
|
70 | 69 | import java.nio.file.attribute.PosixFilePermission;
|
71 | 70 | import java.nio.file.attribute.PosixFilePermissions;
|
72 | 71 | import java.nio.file.attribute.UserPrincipal;
|
| 72 | +import java.security.NoSuchAlgorithmException; |
| 73 | +import java.security.SecureRandom; |
73 | 74 | import java.util.Arrays;
|
74 | 75 | import java.util.Collection;
|
75 | 76 | import java.util.HashMap;
|
|
78 | 79 | import java.util.Locale;
|
79 | 80 | import java.util.Map;
|
80 | 81 | import java.util.Map.Entry;
|
81 |
| -import java.util.Random; |
82 | 82 | import java.util.Set;
|
83 | 83 | import java.util.concurrent.TimeUnit;
|
84 | 84 |
|
@@ -1688,14 +1688,25 @@ public abstract static class ReplaceNode extends RenameNode {
|
1688 | 1688 | @GenerateNodeFactory
|
1689 | 1689 | @TypeSystemReference(PythonArithmeticTypes.class)
|
1690 | 1690 | abstract static class URandomNode extends PythonBuiltinNode {
|
| 1691 | + private static SecureRandom secureRandom; |
| 1692 | + |
| 1693 | + private static SecureRandom createRandomInstance() { |
| 1694 | + try { |
| 1695 | + return SecureRandom.getInstance("NativePRNGNonBlocking"); |
| 1696 | + } catch (NoSuchAlgorithmException e) { |
| 1697 | + throw new IllegalStateException(e); |
| 1698 | + } |
| 1699 | + } |
| 1700 | + |
1691 | 1701 | @Specialization
|
1692 | 1702 | @TruffleBoundary(allowInlining = true)
|
1693 | 1703 | PBytes urandom(int size) {
|
1694 |
| - // size is in bytes |
1695 |
| - BigInteger bigInteger = new BigInteger(size * 8, new Random()); |
1696 |
| - // sign may introduce an extra byte |
1697 |
| - byte[] range = Arrays.copyOfRange(bigInteger.toByteArray(), 0, size); |
1698 |
| - return factory().createBytes(range); |
| 1704 | + if (secureRandom == null) { |
| 1705 | + secureRandom = createRandomInstance(); |
| 1706 | + } |
| 1707 | + byte[] bytes = new byte[size]; |
| 1708 | + secureRandom.nextBytes(bytes); |
| 1709 | + return factory().createBytes(bytes); |
1699 | 1710 | }
|
1700 | 1711 | }
|
1701 | 1712 |
|
|
0 commit comments