Commit 602a3e9
committed
Use the strongest available random source when shuffling
We need to provide cryptographically secure random shuffling.
Looking at Python's implementation of random.shuffle()[1], we see that
(a) it is a straightforward implementation of Durstenfeld's shuffle
algorithm[2] (which is as unbiased as its random number source), and
(b) it allows for a custom `random` function to be passed in.
Hence, AFAICS, we should get a cryptographically secure shuffle as long
as we pass in a `random` function that itself is cryptographically
secure.
Quoting the documentation of the secrets module:
The secrets module provides access to the most secure source of
randomness that your operating system provides.
class secrets.SystemRandom
A class for generating random numbers using the highest-quality
sources provided by the operating system.
Therefore, the best we can do is AFAICS to simply use the `random`
function available from secrets.SystemRandom class.
[1]: https://github.com/python/cpython/blob/v3.6.10rc1/Lib/random.py#L263
[2]: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle#The_modern_algorithm
[3]: https://docs.python.org/3.6/library/secrets.html#random-numbers1 parent 1d167bd commit 602a3e9
1 file changed
Lines changed: 15 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
80 | 92 | | |
81 | 93 | | |
82 | 94 | | |
| |||
130 | 142 | | |
131 | 143 | | |
132 | 144 | | |
133 | | - | |
134 | | - | |
| 145 | + | |
135 | 146 | | |
136 | 147 | | |
137 | 148 | | |
| |||
310 | 321 | | |
311 | 322 | | |
312 | 323 | | |
313 | | - | |
| 324 | + | |
314 | 325 | | |
315 | 326 | | |
316 | 327 | | |
| |||
451 | 462 | | |
452 | 463 | | |
453 | 464 | | |
454 | | - | |
| 465 | + | |
0 commit comments