root: src: Random: support multiple types
Allows signed/alternative-unsigned types (impl-specific).
This commit is contained in:
@@ -54,6 +54,9 @@ namespace impl
|
|||||||
//! \since docker-finance 1.0.0
|
//! \since docker-finance 1.0.0
|
||||||
namespace common
|
namespace common
|
||||||
{
|
{
|
||||||
|
|
||||||
|
namespace type = docker_finance::internal::type;
|
||||||
|
|
||||||
//! \brief Generic Random implementation common among all library-specific implementations
|
//! \brief Generic Random implementation common among all library-specific implementations
|
||||||
//! \ingroup cpp_API_impl
|
//! \ingroup cpp_API_impl
|
||||||
//! \since docker-finance 1.0.0
|
//! \since docker-finance 1.0.0
|
||||||
@@ -76,15 +79,14 @@ class RandomImpl : public ::docker_finance::internal::Random<t_impl>
|
|||||||
t_random generate_impl()
|
t_random generate_impl()
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_same_v<t_random, uint32_t>, "Random interface has changed");
|
type::is_real_integral<t_random>::value,
|
||||||
|
"Random interface has changed");
|
||||||
|
|
||||||
return t_impl::that()->template generate<t_random>();
|
return t_impl::that()->template generate<t_random>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace common
|
} // namespace common
|
||||||
|
|
||||||
namespace type = docker_finance::internal::type;
|
|
||||||
|
|
||||||
//! \namespace docker_finance::crypto::impl::botan
|
//! \namespace docker_finance::crypto::impl::botan
|
||||||
//! \since docker-finance 1.0.0
|
//! \since docker-finance 1.0.0
|
||||||
namespace botan
|
namespace botan
|
||||||
@@ -118,7 +120,13 @@ class Random : public common::RandomImpl<botan::Random>
|
|||||||
t_random generate_impl()
|
t_random generate_impl()
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_same_v<t_random, uint32_t>, "Random interface has changed");
|
type::is_real_integral<t_random>::value,
|
||||||
|
"Random interface has changed");
|
||||||
|
|
||||||
|
static_assert(
|
||||||
|
std::is_same_v<t_random, uint16_t>
|
||||||
|
|| std::is_same_v<t_random, uint32_t>,
|
||||||
|
"Invalid type (only uint16_t or uint32_t supported)");
|
||||||
|
|
||||||
THROW_IF(!m_csprng.is_seeded(), type::RuntimeError, "Botan is not seeded")
|
THROW_IF(!m_csprng.is_seeded(), type::RuntimeError, "Botan is not seeded")
|
||||||
|
|
||||||
@@ -170,8 +178,15 @@ class Random : public common::RandomImpl<cryptopp::Random>
|
|||||||
template <typename t_random>
|
template <typename t_random>
|
||||||
t_random generate_impl()
|
t_random generate_impl()
|
||||||
{
|
{
|
||||||
|
// NOTE: signed/unsigned types are supported (up to uint32_t)
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_same_v<t_random, uint32_t>, "Random interface has changed");
|
type::is_real_integral<t_random>::value,
|
||||||
|
"Random interface has changed");
|
||||||
|
|
||||||
|
static_assert(
|
||||||
|
!std::is_same_v<t_random, int64_t>
|
||||||
|
&& !std::is_same_v<t_random, uint64_t>,
|
||||||
|
"Invalid type (no greater than uint32_t allowed)");
|
||||||
|
|
||||||
return m_csprng.GenerateWord32(0, std::numeric_limits<t_random>::max());
|
return m_csprng.GenerateWord32(0, std::numeric_limits<t_random>::max());
|
||||||
}
|
}
|
||||||
@@ -226,7 +241,14 @@ class Random : public common::RandomImpl<libsodium::Random>
|
|||||||
t_random generate_impl()
|
t_random generate_impl()
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_same_v<t_random, uint32_t>, "Random interface has changed");
|
type::is_real_integral<t_random>::value,
|
||||||
|
"Random interface has changed");
|
||||||
|
|
||||||
|
// signed or uint64_t types are not supported
|
||||||
|
static_assert(
|
||||||
|
std::is_same_v<t_random, uint16_t>
|
||||||
|
|| std::is_same_v<t_random, uint32_t>,
|
||||||
|
"Invalid type (only uint16_t or uint32_t supported)");
|
||||||
|
|
||||||
return ::randombytes_random();
|
return ::randombytes_random();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#define CONTAINER_SRC_ROOT_SRC_RANDOM_HH_
|
#define CONTAINER_SRC_ROOT_SRC_RANDOM_HH_
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "./internal/impl/random.hh"
|
#include "./internal/impl/random.hh"
|
||||||
|
|
||||||
@@ -64,9 +65,17 @@ class Random final : public t_impl
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
//! \brief Random number generator
|
//! \brief Random number generator
|
||||||
//! \details Returns a random value between 0 and 0xFFFFFFFF
|
//! \details Returns a random value between 0 and 0xFFFFFFFF (depending on type)
|
||||||
//! \todo May as well enable_if uint32_t for consistency
|
//! \warning Signed types will return positive value (per implementation)
|
||||||
uint32_t generate() { return t_impl::template generate<uint32_t>(); }
|
//! \tparam t_random Integral type of random number
|
||||||
|
//! \return t_random Random number of type t_random
|
||||||
|
template <
|
||||||
|
typename t_random = uint32_t,
|
||||||
|
std::enable_if_t<type::is_real_integral<t_random>::value, bool> = true>
|
||||||
|
t_random generate()
|
||||||
|
{
|
||||||
|
return t_impl::template generate<t_random>();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} // namespace common
|
} // namespace common
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user