rand/system;win32: add an RtlGenRandom generator

This commit is contained in:
Danny Robson 2019-06-20 16:58:32 +10:00
parent e0350f912b
commit 3cace81ea5
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,37 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
*/
#include "system_win32.hpp"
#include "win32/except.hpp"
#include <windows.h>
#include <ntsecapi.h>
using cruft::rand::win32::rtlgenrandom;
///////////////////////////////////////////////////////////////////////////////
rtlgenrandom::rtlgenrandom ()
{ ; }
//-----------------------------------------------------------------------------
rtlgenrandom::rtlgenrandom (std::string const &)
{ ; }
///////////////////////////////////////////////////////////////////////////////
rtlgenrandom::result_type
rtlgenrandom::operator() ()
{
result_type res;
if (RtlGenRandom (&res, sizeof (res)) != TRUE)
cruft::win32::error::throw_code ();
return res;
}

View File

@ -6,3 +6,34 @@
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include <limits>
#include <string>
namespace cruft::rand {
namespace win32 {
/// Models std::random_device and is suitable as a drop-in replacement.
///
/// This uses the RtlGenRandom function which is currently deprecated.
class rtlgenrandom {
public:
using result_type = unsigned;
rtlgenrandom ();
rtlgenrandom (std::string const&);
rtlgenrandom (rtlgenrandom const &) = delete;
rtlgenrandom& operator= (rtlgenrandom const&) = delete;
result_type operator() (void);
double entropy (void) const noexcept;
static constexpr result_type min (void) { return std::numeric_limits<result_type>::min (); }
static constexpr result_type max (void) { return std::numeric_limits<result_type>::max (); }
};
}
using system = win32::rtlgenrandom;
}