4 ms·
How to safely generate random numbers on Windows[1][2]: #include <windows.h> #define SystemFunction036 NTAPI SystemFunction036 #include <ntsecapi.h> #u
by ryuuchin 10y ago
How to safely generate random numbers on Windows[1][2]:
#include <windows.h>
#define SystemFunction036 NTAPI SystemFunction036
#include <ntsecapi.h>
#undef SystemFunction036
RtlGenRandom(buf, size);
This is more convenient than both rand_s[3] and CryptGenRandom[4]. The former can only generate one byte per call and the latter requires you to create a CSP (cryptographic service provider) before you can call it (which means you have to destroy the CSP when you're done using it). In the end these all call RtlGenRandom.
RtlGenRandom uses a CSPRNG specified in NIST 800-90[4]. It uses AES256 in CTR mode AFAIK.
[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...
[3] https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
[4] https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3...
- CiPHPerCoder 10y agoFor anyone not writing C/C++ but still needs a CSPRNG: https://paragonie.com/blog/2016/05/how-generate-secure-random-numbers-in-various-programming-languages https://paragonie.com/blog/2016/05/how-generate-secure-rando...