1 // Boost seed_rng.hpp header file ----------------------------------------------//
3 // Copyright 2007 Andy Tompkins.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
9 // 09 Nov 2007 - Initial Revision
10 // 25 Feb 2008 - moved to namespace boost::uuids::detail
11 // 28 Nov 2009 - disabled deprecated warnings for MSVC
13 // seed_rng models a UniformRandomNumberGenerator (see Boost.Random).
14 // Random number generators are hard to seed well. This is intended to provide
15 // good seed values for random number generators.
16 // It creates random numbers from a sha1 hash of data from a variary of sources,
17 // all of which are standard function calls. It produces random numbers slowly.
18 // Peter Dimov provided the details of sha1_random_digest_().
19 // see http://archives.free.net.ph/message/20070507.175609.4c4f503a.en.html
21 #ifndef BOOST_UUID_SEED_RNG_HPP
22 #define BOOST_UUID_SEED_RNG_HPP
24 #include <boost/config.hpp>
25 #include <cstring> // for memcpy
27 #include <ctime> // for time_t, time, clock_t, clock
28 #include <cstdlib> // for rand
29 #include <cstdio> // for FILE, fopen, fread, fclose
30 #include <boost/uuid/sha1.hpp>
31 //#include <boost/nondet_random.hpp> //forward declare boost::random::random_device
33 // can't use boost::generator_iterator since boost::random number seed(Iter&, Iter)
34 // functions need a last iterator
35 //#include <boost/generator_iterator.hpp>
36 # include <boost/iterator/iterator_facade.hpp>
39 #pragma warning(push) // Save warning settings.
40 #pragma warning(disable : 4996) // Disable deprecated std::fopen
43 #ifdef BOOST_NO_STDC_NAMESPACE
58 // forward declare random number generators
59 namespace boost { namespace random {
61 }} //namespace boost::random
67 // should this be part of Boost.Random?
71 typedef unsigned int result_type;
72 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
73 //BOOST_STATIC_CONSTANT(unsigned int, min_value = 0);
74 //BOOST_STATIC_CONSTANT(unsigned int, max_value = UINT_MAX);
77 // note: rd_ intentionally left uninitialized
80 , random_(std::fopen( "/dev/urandom", "rb" ))
90 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const
92 return (std::numeric_limits<result_type>::min)();
94 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const
96 return (std::numeric_limits<result_type>::max)();
99 result_type operator()()
101 if (rd_index_ >= 5) {
103 sha1_random_digest_();
108 return rd_[rd_index_++];
112 inline void ignore_size(size_t) {}
114 static unsigned int * sha1_random_digest_state_()
116 static unsigned int state[ 5 ];
120 void sha1_random_digest_()
122 boost::uuids::detail::sha1 sha;
124 unsigned int * ps = sha1_random_digest_state_();
126 unsigned int state[ 5 ];
127 std::memcpy( state, ps, sizeof( state ) ); // harmless data race
129 sha.process_bytes( (unsigned char const*)state, sizeof( state ) );
130 sha.process_bytes( (unsigned char const*)&ps, sizeof( ps ) );
133 std::time_t tm = std::time( 0 );
134 sha.process_bytes( (unsigned char const*)&tm, sizeof( tm ) );
138 std::clock_t ck = std::clock();
139 sha.process_bytes( (unsigned char const*)&ck, sizeof( ck ) );
144 { static_cast<unsigned int>(std::rand())
145 , static_cast<unsigned int>(std::rand())
146 , static_cast<unsigned int>(std::rand())
148 sha.process_bytes( (unsigned char const*)rn, sizeof( rn ) );
152 // intentionally left uninitialized
153 unsigned char buffer[ 20 ];
157 ignore_size(std::fread( buffer, 1, 20, random_ ));
160 // using an uninitialized buffer[] if fopen fails
161 // intentional, we rely on its contents being random
162 sha.process_bytes( buffer, sizeof( buffer ) );
166 // *p is intentionally left uninitialized
167 unsigned int * p = new unsigned int;
169 sha.process_bytes( (unsigned char const*)p, sizeof( *p ) );
170 sha.process_bytes( (unsigned char const*)&p, sizeof( p ) );
175 sha.process_bytes( (unsigned char const*)rd_, sizeof( rd_ ) );
177 unsigned int digest[ 5 ];
178 sha.get_digest( digest );
180 for( int i = 0; i < 5; ++i )
182 // harmless data race
183 ps[ i ] ^= digest[ i ];
184 rd_[ i ] ^= digest[ i ];
193 private: // make seed_rng noncopyable
194 seed_rng(seed_rng const&);
195 seed_rng& operator=(seed_rng const&);
198 // almost a copy of boost::generator_iterator
199 // but default constructor sets m_g to NULL
200 template <class Generator>
201 class generator_iterator
202 : public iterator_facade<
203 generator_iterator<Generator>
204 , typename Generator::result_type
205 , single_pass_traversal_tag
206 , typename Generator::result_type const&
209 typedef iterator_facade<
210 generator_iterator<Generator>
211 , typename Generator::result_type
212 , single_pass_traversal_tag
213 , typename Generator::result_type const&
217 generator_iterator() : m_g(NULL), m_value(0) {}
218 generator_iterator(Generator* g) : m_g(g), m_value((*m_g)()) {}
225 const typename Generator::result_type&
231 bool equal(generator_iterator const& y) const
233 return this->m_g == y.m_g && this->m_value == y.m_value;
238 typename Generator::result_type m_value;
241 // seed() seeds a random number generator with good seed values
243 template <typename UniformRandomNumberGenerator>
244 inline void seed(UniformRandomNumberGenerator& rng)
247 generator_iterator<seed_rng> begin(&seed_gen);
248 generator_iterator<seed_rng> end;
249 rng.seed(begin, end);
252 // random_device does not / can not be seeded
254 inline void seed<boost::random::random_device>(boost::random::random_device&) {}
256 // random_device does not / can not be seeded
258 inline void seed<seed_rng>(seed_rng&) {}
260 }}} //namespace boost::uuids::detail
262 #if defined(_MSC_VER)
263 #pragma warning(pop) // Restore warnings to previous state.