scherlock
scherlock

Reputation: 217

Simulating /dev/random on Windows

I'm trying to port python code from linux to windows right now. In various places random numbers are generateted by reading from /dev/random. Is there a way to simulate /dev/random on Windows?

I'm looking for a solution that would keep the code useable on linux...

Upvotes: 7

Views: 4615

Answers (4)

Tom Rutchik
Tom Rutchik

Reputation: 1702

/dev/random is available if you install Cygwin. In the Cygwin console window you'll be able to run the command:

tom@myHost ~
$ hexdump -C -n 8 /dev/random
00000000  4f 9d 57 cc 9a 01 aa cb                           |O.W.....|
00000008

And assuming you add the Cygwin's bin directory to your path, you can also run that command in the Window Command Prompt window, but it'll only work using Cygwin commands.

Microsoft Windows [Version 10.0.18363.778]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\tom>hexdump -C -n 8 /dev/random
00000000  01 99 93 1d 51 f0 dd f4                           |....Q...|
00000008

Don't know if this helps you or not! I don't think you'll be able to use it as device file that you can read from inside an application without some kind of trick such as: see https://www.codeproject.com/articles/66235/interacting-with-shell-applications-made-easy

Upvotes: 0

Gareth Latty
Gareth Latty

Reputation: 89057

If you are using Python, why do you care about the specific implementation? Just use the random module and let it deal with it.

Beyond that, (if you can't rely on software state) os.urandom provides os-based random values:

On a UNIX-like system this will query /dev/urandom, and on Windows it will use CryptGenRandom.

(Note that random.SystemRandom provides a nice interface for this).

If you are really serious about it being cryptographically random, you might want to check out PyCrypto.

Upvotes: 13

Soz
Soz

Reputation: 963

You could call random.SystemRandom instead. This will use CryptGenRandom on Windows and /dev/urandom on Linux.

Otherwise, there's always Cygwin's /dev/random?

Upvotes: 7

Steve
Steve

Reputation: 7108

You could use random from Python's standard library.

Upvotes: 1

Related Questions