2
0
mirror of https://github.com/boostorg/uuid.git synced 2026-01-19 04:42:16 +00:00

Use chacha20_12 in random_generator; update definitions of random_generator_mt19937 and random_generator_pure

This commit is contained in:
Peter Dimov
2024-04-23 04:41:35 +03:00
parent 059bf0687a
commit a81ba56f1b
6 changed files with 28 additions and 96 deletions

View File

@@ -5,6 +5,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <limits>
#include <cstdint>
#include <cstddef>
@@ -120,6 +121,16 @@ public:
state_[ 13 ] = 0;
}
static constexpr result_type min()
{
return std::numeric_limits<result_type>::min();
}
static constexpr result_type max()
{
return std::numeric_limits<result_type>::max();
}
result_type operator()()
{
if( index_ == 16 )

View File

@@ -1,75 +1,33 @@
#ifndef BOOST_UUID_RANDOM_GENERATOR_HPP_INCLUDED
#define BOOST_UUID_RANDOM_GENERATOR_HPP_INCLUDED
// Boost random_generator.hpp header file ----------------------------------------------//
// Copyright 2010 Andy Tompkins.
// Copyright 2017 James E. King III
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/uuid/basic_random_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/detail/random_provider.hpp>
#include <memory>
#include <cstring>
#include <cstdint>
#include <boost/uuid/detail/random_device.hpp>
#include <boost/uuid/detail/chacha20.hpp>
namespace boost {
namespace uuids {
//! \brief a far less complex random generator that uses
//! operating system provided entropy which will
//! satisfy the majority of use cases
class random_generator_pure
// only provided for compatibility with 1.85
class random_generator_mt19937: public basic_random_generator<std::mt19937>
{
private:
std::unique_ptr<detail::random_provider> prov_;
public:
typedef uuid result_type;
random_generator_pure(): prov_( new detail::random_provider )
{
}
random_generator_pure(random_generator_pure&& that) = default;
random_generator_pure& operator= (random_generator_pure&& that) = default;
//! \returns a random, valid uuid
//! \throws entropy_error
result_type operator()()
{
result_type result;
std::uint32_t tmp[ 4 ];
prov_->generate( tmp + 0, tmp + 4 );
std::memcpy( result.data, tmp, 16 );
// set variant
// must be 0b10xxxxxx
*(result.begin() + 8) &= 0xBF;
*(result.begin() + 8) |= 0x80;
// set version
// must be 0b0100xxxx
*(result.begin() + 6) &= 0x4F; //0b01001111
*(result.begin() + 6) |= 0x40; //0b01000000
return result;
}
};
#if defined(BOOST_UUID_RANDOM_GENERATOR_COMPAT)
typedef basic_random_generator<std::mt19937> random_generator;
#else
typedef random_generator_pure random_generator;
typedef basic_random_generator<std::mt19937> random_generator_mt19937;
#endif
// only provided for compatibility with 1.85
class random_generator_pure: public basic_random_generator<detail::random_device>
{
};
// the default random generator
class random_generator: public basic_random_generator<detail::chacha20_12>
{
};
}} // namespace boost::uuids

View File

@@ -116,9 +116,6 @@ run test_detail_chacha20.cpp ;
# compile-fail tests
compile-fail compile-fail/random_generator_no_copy_assign.cpp ;
compile-fail compile-fail/random_generator_no_copy_ctor.cpp ;
compile-fail compile-fail/random_provder_no_copy_ctor.cpp ;
compile-fail compile-fail/random_provider_no_copy_assign.cpp ;

View File

@@ -1,17 +0,0 @@
// (c) Copyright Andrey Semashev 2018
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// The test verifies that random_generator is not copy assignable
#include <boost/uuid/random_generator.hpp>
int main()
{
boost::uuids::random_generator uuid_gen1, uuid_gen2;
uuid_gen2 = uuid_gen1;
return 1;
}

View File

@@ -1,17 +0,0 @@
// (c) Copyright Andrey Semashev 2018
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// The test verifies that random_generator is not copy constructible
#include <boost/uuid/random_generator.hpp>
int main()
{
boost::uuids::random_generator uuid_gen1;
boost::uuids::random_generator uuid_gen2(uuid_gen1);
return 1;
}

View File

@@ -74,7 +74,7 @@ int main()
{
std::mt19937 rng;
random_generator_mt19937 gen( &rng );
basic_random_generator<std::mt19937> gen( &rng );
for( int i = 0; i < 16; ++i )
{