diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-05-06 18:00:41 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-05-06 18:00:41 (GMT) |
commit | 4c58e9a2d266c23a2060edd08081bf86ebd1862c (patch) | |
tree | 0030a4f3360c9da905165db1feed18997481fee5 /3rdParty/Boost/src/boost/uuid/random_generator.hpp | |
parent | c2cf7f0e59c7880a9ce979d8a45d97442c705110 (diff) | |
download | swift-contrib-4c58e9a2d266c23a2060edd08081bf86ebd1862c.zip swift-contrib-4c58e9a2d266c23a2060edd08081bf86ebd1862c.tar.bz2 |
Use UUIDs as nonce when authenticating with SCRAM-SHA-1.
Diffstat (limited to '3rdParty/Boost/src/boost/uuid/random_generator.hpp')
-rw-r--r-- | 3rdParty/Boost/src/boost/uuid/random_generator.hpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/3rdParty/Boost/src/boost/uuid/random_generator.hpp b/3rdParty/Boost/src/boost/uuid/random_generator.hpp new file mode 100644 index 0000000..4d11f6b --- /dev/null +++ b/3rdParty/Boost/src/boost/uuid/random_generator.hpp @@ -0,0 +1,118 @@ +// Boost random_generator.hpp header file ----------------------------------------------// + +// Copyright 2010 Andy Tompkins. +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_UUID_RANDOM_GENERATOR_HPP +#define BOOST_UUID_RANDOM_GENERATOR_HPP + +#include <boost/uuid/uuid.hpp> +#include <boost/uuid/seed_rng.hpp> +#include <boost/random/uniform_int.hpp> +#include <boost/random/variate_generator.hpp> +#include <boost/random/mersenne_twister.hpp> +#include <boost/assert.hpp> +#include <boost/shared_ptr.hpp> +#include <limits> + +namespace boost { +namespace uuids { + +// generate a random-based uuid +template <typename UniformRandomNumberGenerator> +class basic_random_generator { +private: + typedef uniform_int<unsigned long> distribution_type; + typedef variate_generator<UniformRandomNumberGenerator*, distribution_type> generator_type; + + struct null_deleter + { + void operator()(void const *) const {} + }; + +public: + typedef uuid result_type; + + // default constructor creates the random number generator + basic_random_generator() + : pURNG(new UniformRandomNumberGenerator) + , generator + ( pURNG.get() + , distribution_type + ( (std::numeric_limits<unsigned long>::min)() + , (std::numeric_limits<unsigned long>::max)() + ) + ) + { + // seed the random number generator + detail::seed(*pURNG); + } + + // keep a reference to a random number generator + // don't seed a given random number generator + explicit basic_random_generator(UniformRandomNumberGenerator& gen) + : pURNG(&gen, null_deleter()) + , generator + ( pURNG.get() + , distribution_type + ( (std::numeric_limits<unsigned long>::min)() + , (std::numeric_limits<unsigned long>::max)() + ) + ) + {} + + // keep a pointer to a random number generator + // don't seed a given random number generator + explicit basic_random_generator(UniformRandomNumberGenerator* pGen) + : pURNG(pGen, null_deleter()) + , generator + ( pURNG.get() + , distribution_type + ( (std::numeric_limits<unsigned long>::min)() + , (std::numeric_limits<unsigned long>::max)() + ) + ) + { + BOOST_ASSERT(pURNG); + } + + uuid operator()() + { + uuid u; + + int i=0; + unsigned long random_value = generator(); + for (uuid::iterator it=u.begin(); it!=u.end(); ++it, ++i) { + if (i==sizeof(unsigned long)) { + random_value = generator(); + i = 0; + } + + *it = ((random_value >> (i*8)) & 0xFF); + } + + // set variant + // must be 0b10xxxxxx + *(u.begin()+8) &= 0xBF; + *(u.begin()+8) |= 0x80; + + // set version + // must be 0b0100xxxx + *(u.begin()+6) &= 0x4F; //0b01001111 + *(u.begin()+6) |= 0x40; //0b01000000 + + return u; + } + +private: + shared_ptr<UniformRandomNumberGenerator> pURNG; + generator_type generator; +}; + +typedef basic_random_generator<mt19937> random_generator; + +}} // namespace boost::uuids + +#endif //BOOST_UUID_RANDOM_GENERATOR_HPP |