5 ms·
I wonder what to use on Windows. Because sometimes you maybe need robust randomness on a popular desktop.
by nine_k 11y ago
I wonder what to use on Windows. Because sometimes you maybe need robust randomness on a popular desktop.
- FiloSottile 11y agoCryptGenRandom. It's the Windows equivalent of /dev/urandom.
- veeti 11y agoCryptGenRandom [1] would be the equivalent. [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3...
- cpeterso 11y agoOr rand_s(), which wraps CryptGenRandom() with a more convenient API: https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
- ryuuchin 11y agoRtlGenRandom[1] is probably the most convenient way on Windows. Does require certain definitions when including the header file for it[2] but otherwise does not require a CSP context and can generate any number of CSPRNG bytes. [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa387694(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3... [2] https://boringssl.googlesource.com/boringssl/+/master/crypto/rand/windows.c#26 https://boringssl.googlesource.com/boringssl/+/master/crypto...
- cpeterso 11y agoYou're right. Calling RtlGenRandom() directly is probably the best option. I forgot that rand_s() calls RtlGenRandom(), not CryptGenRandom(). Plus both Firefox and Chrome ran into rare crashes when some bad third-party software (antivirus or malware) injects advapi32.dll hooks, causing rand_s() to crash when it tries to load advapi32.dll in order to call RtlGenRandom(). Firefox bugs: https://bugzil.la/1240589 https://bugzil.la/1240589, https://bugzil.la/1167248 https://bugzil.la/1167248, https://bugzil.la/694344 https://bugzil.la/694344 Chrome bug: https://crbug.com/348400 https://crbug.com/348400
- ryuuchin 11y agoYou can also specify the number of bytes you want with RtlGenRandom. rand_s only gives an unsigned int per call. Not a huge deal but you may be able to cut down on some function call overhead depending on how stuff gets inlined/optimized.
- PascLeRasc 11y agoUbuntu is a pretty good distro to start using linux with, try that.
- darkstar999 11y agoYou're missing the point. Sometimes you need to write software for Windows users.
- ryuuchin 11y agoCryptGenRandom[1] or RtlGenRandom[2]. Although CryptGenRandom is the offical version they probably would rather you use I'm fairly certain that most people are more likely to use RtlGenRandom because it's accessing the same thing but doesn't require a CSP context in order to use it (you can just call it straight up). You have to include ntsecapi.h with some fixing definitions[3] to use RtlGenRandom. [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3... [2] https://msdn.microsoft.com/en-us/library/windows/desktop/aa387694(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3... [3] https://boringssl.googlesource.com/boringssl/+/master/crypto/rand/windows.c#26 https://boringssl.googlesource.com/boringssl/+/master/crypto...