random_provider_getentropy.ipp 933 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Copyright (c) 2017 James E. King III
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // https://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // getentropy() capable platforms
  9. //
  10. #include <boost/config.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <cerrno>
  13. #include <cstddef>
  14. #include <unistd.h>
  15. namespace boost {
  16. namespace uuids {
  17. namespace detail {
  18. class random_provider_base
  19. {
  20. public:
  21. //! Obtain entropy and place it into a memory location
  22. //! \param[in] buf the location to write entropy
  23. //! \param[in] siz the number of bytes to acquire
  24. void get_random_bytes(void *buf, std::size_t siz)
  25. {
  26. int res = getentropy(buf, siz);
  27. if (BOOST_UNLIKELY(-1 == res))
  28. {
  29. int err = errno;
  30. BOOST_THROW_EXCEPTION(entropy_error(err, "getentropy"));
  31. }
  32. }
  33. };
  34. } // detail
  35. } // uuids
  36. } // boost