diff options
| author | Nils Maier <maierman@web.de> | 2014-12-30 18:17:44 +0100 |
|---|---|---|
| committer | Nils Maier <maierman@web.de> | 2015-01-20 22:37:24 +0100 |
| commit | 81bdd5f61a8f8da53e22d68190be700902b7dbae (patch) | |
| tree | f706934a5e5e208d8d09490d216fe03f10b38654 /src/SimpleRandomizer.h | |
| parent | 747131a06c1f1d92a5312a103f4446fabedb6cf7 (diff) | |
Revise getRandom facilities
Use one of the following to provide random bytes:
- Windows CryptGenRandom
- Linux getrandom (syscall interface to urandom, without nasty corner
cases such as file descriptor exhaustion or re-linked /dev/urandom)
- std::device_random (C++ random device, which usually will be urandom)
This also equalizes util::getRandom and SimpleRandomizer (the former
will now use the latter) instead of having essentially two different
PRNG interfaces with potentially different quality.
Closes GH-320
Diffstat (limited to 'src/SimpleRandomizer.h')
| -rw-r--r-- | src/SimpleRandomizer.h | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/src/SimpleRandomizer.h b/src/SimpleRandomizer.h index fa576306..65ac2094 100644 --- a/src/SimpleRandomizer.h +++ b/src/SimpleRandomizer.h @@ -41,30 +41,32 @@ #include <random> #ifdef __MINGW32__ -# include <wincrypt.h> -#endif // __MINGW32__ +# include <wincrypt.h> +#endif namespace aria2 { class SimpleRandomizer : public Randomizer { private: static std::unique_ptr<SimpleRandomizer> randomizer_; + SimpleRandomizer(); +private: #ifdef __MINGW32__ - HCRYPTPROV cryProvider_; -#else // !__MINGW32__ - std::minstd_rand eng_; -#endif //!__MINGW32__ + HCRYPTPROV provider_; +#elif defined(HAVE_GETRANDOM_INTERFACE) + // Nothing special needed +#else + std::random_device dev_; +#endif // ! __MINGW32__ - SimpleRandomizer(); public: + typedef std::random_device::result_type result_type; static const std::unique_ptr<SimpleRandomizer>& getInstance(); virtual ~SimpleRandomizer(); - void init(); - /** * Returns random number in [0, to). */ @@ -72,7 +74,32 @@ public: void getRandomBytes(unsigned char *buf, size_t len); - long int operator()(long int to); + long int operator()(long int to) + { + return getRandomNumber(to); + } + + result_type operator()() + { + result_type rv; + getRandomBytes(reinterpret_cast<unsigned char*>(&rv), sizeof(rv)); + return rv; + } + + static constexpr result_type min() + { + return std::numeric_limits<result_type>::min(); + } + + static constexpr result_type max() + { + return std::numeric_limits<result_type>::max(); + } + + static double entropy() + { + return 0.0; + } }; } // namespace aria2 |
