root: src: Random: support multiple types

Allows signed/alternative-unsigned types (impl-specific).
This commit is contained in:
2024-07-23 16:00:01 -07:00
parent 1092cd4adc
commit 879eb6f785
2 changed files with 40 additions and 9 deletions

View File

@@ -54,6 +54,9 @@ namespace impl
//! \since docker-finance 1.0.0
namespace common
{
namespace type = docker_finance::internal::type;
//! \brief Generic Random implementation common among all library-specific implementations
//! \ingroup cpp_API_impl
//! \since docker-finance 1.0.0
@@ -76,15 +79,14 @@ class RandomImpl : public ::docker_finance::internal::Random<t_impl>
t_random generate_impl()
{
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>();
}
};
} // namespace common
namespace type = docker_finance::internal::type;
//! \namespace docker_finance::crypto::impl::botan
//! \since docker-finance 1.0.0
namespace botan
@@ -118,7 +120,13 @@ class Random : public common::RandomImpl<botan::Random>
t_random generate_impl()
{
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")
@@ -170,8 +178,15 @@ class Random : public common::RandomImpl<cryptopp::Random>
template <typename t_random>
t_random generate_impl()
{
// NOTE: signed/unsigned types are supported (up to uint32_t)
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());
}
@@ -226,7 +241,14 @@ class Random : public common::RandomImpl<libsodium::Random>
t_random generate_impl()
{
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();
}

View File

@@ -25,6 +25,7 @@
#define CONTAINER_SRC_ROOT_SRC_RANDOM_HH_
#include <cstdint>
#include <type_traits>
#include "./internal/impl/random.hh"
@@ -64,9 +65,17 @@ class Random final : public t_impl
public:
//! \brief Random number generator
//! \details Returns a random value between 0 and 0xFFFFFFFF
//! \todo May as well enable_if uint32_t for consistency
uint32_t generate() { return t_impl::template generate<uint32_t>(); }
//! \details Returns a random value between 0 and 0xFFFFFFFF (depending on type)
//! \warning Signed types will return positive value (per implementation)
//! \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