2 ms·
Sure. FWIW, my favorite is APIs like Python's os.urandom which just always does the right thing already (including, IIRC, in very recent versions, getentropy()/
by lvh 9y ago
Sure. FWIW, my favorite is APIs like Python's os.urandom which just always does the right thing already (including, IIRC, in very recent versions, getentropy()/getrandom()/arc4random_buf() or whatever where available).
I feel like currently there are slightly fewer ways you could open up /dev/urandom and screw it up (you mention a few valid ones -- but now you're doing feature detection) and it's incidentally cross-platform (dev on macOS, deploy on Linux: super common) so I'm pretty comfortable with it still being the default. Maybe not in the 2020 version -- we'll see. I mean sure you can run out of fds and maybe /dev/urandom isn't (1, 9) but if those things happen you're already on your last legs :-)
- deathanatos 9y agoIf you have a modern version of Python, you can do: import secrets secrets.token_bytes() # returns a randomized token You can pass in the number of bytes you want, but I would think it would be better to rely on the default (presently 256 bit). This module is intended for cryptographic purposes. There are also helper functions to get a hex string or a URL safe string back. https://docs.python.org/3/library/secrets.html https://docs.python.org/3/library/secrets.html