| 6 |
| 7 | #include <windows.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 |
| 11 | // #define needed to link in RtlGenRandom(), a.k.a. SystemFunction036. See the |
| 12 | // "Community Additions" comment on MSDN here: |
| 13 | // http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx |
| 14 | #define SystemFunction036 NTAPI SystemFunction036 |
| 15 | #include <NTSecAPI.h> |
| 16 | #undef SystemFunction036 |
| 17 |
| 18 | #include <algorithm> |
| 19 | #include <limits> |
| 20 |
| 21 | #include "base/logging.h" |
| 22 |
| 23 | namespace base { |
| 24 |
| 25 | void RandBytes(void* output, size_t output_length) { |
| 26 | char* output_ptr = static_cast<char*>(output); |
| 27 | while (output_length > 0) { |
| 28 | const ULONG output_bytes_this_pass = static_cast<ULONG>(std::min( |
| 29 | output_length, static_cast<size_t>(std::numeric_limits<ULONG>::max()))); |
| 30 | const bool success = |
| 31 | RtlGenRandom(output_ptr, output_bytes_this_pass) != FALSE; |
| 32 | CHECK(success); |
| 33 | output_length -= output_bytes_this_pass; |
| 34 | output_ptr += output_bytes_this_pass; |
| 35 | } |
| 36 | } |
| 37 |
| 38 | } // namespace base |
| 39 |