root: macro: crypto: add random.C

Generates CSPRNG numbers of various types (impl-specific).
This commit is contained in:
2024-07-23 18:06:16 -07:00
parent 480b42e273
commit 7901421fd4

View File

@@ -0,0 +1,141 @@
// docker-finance | modern accounting for the power-user
//
// Copyright (C) 2024 Aaron Fiore (Founder, Evergreen Crypto LLC)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! \file
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
//! \since docker-finance 1.0.0
#ifndef CONTAINER_SRC_ROOT_MACRO_CRYPTO_RANDOM_C_
#define CONTAINER_SRC_ROOT_MACRO_CRYPTO_RANDOM_C_
#include <iostream>
#include <map>
#include <string>
#include "../common/crypto.hh"
//! \namespace docker_finance
//! \since docker-finance 1.0.0
namespace docker_finance
{
//! \namespace docker_finance::macro
//! \brief ROOT macros
//! \since docker-finance 1.0.0
namespace macro
{
//! \namespace docker_finance::macro::crypto
//! \brief ROOT cryptographic-based macros
//! \since docker-finance 1.0.0
namespace crypto
{
namespace common = ::docker_finance::macro::common;
namespace libsodium = common::crypto::libsodium;
namespace cryptopp = common::crypto::cryptopp;
namespace botan = common::crypto::botan;
//! \brief CSPRNG macro
class Random final
{
using t_rng = std::map<std::string, size_t>;
public:
Random() = default;
~Random() = default;
Random(const Random&) = delete;
Random& operator=(const Random&) = delete;
Random(Random&&) = default;
Random& operator=(Random&&) = default;
protected:
//! \brief Botan RNG
//! \return t_rng Random map to print (label, num)
static t_rng botan_generate()
{
t_rng rng;
rng["int16_t (unsupported)"] = 0;
rng["int32_t (unsupported)"] = 0;
rng["uint16_t"] = botan::g_Random->generate<uint16_t>();
rng["uint32_t"] = botan::g_Random->generate<uint32_t>();
return rng;
}
//! \brief Crypto++ RNG
//! \return t_rng Random map to print (label, num)
static t_rng cryptopp_generate()
{
t_rng rng;
rng["int16_t"] = cryptopp::g_Random->generate<int16_t>();
rng["int32_t"] = cryptopp::g_Random->generate<int32_t>();
rng["uint16_t"] = cryptopp::g_Random->generate<uint16_t>();
rng["uint32_t"] = cryptopp::g_Random->generate<uint32_t>();
return rng;
}
//! \brief libsodium RNG
//! \return t_rng Random map to print (label, num)
static t_rng libsodium_generate()
{
t_rng rng;
rng["int16_t (unsupported)"] = 0;
rng["int32_t (unsupported)"] = 0;
rng["uint16_t"] = libsodium::g_Random->generate<uint16_t>();
rng["uint32_t"] = libsodium::g_Random->generate<uint32_t>();
return rng;
}
public:
//! \brief Print t_rng Random map of CSPRNG numbers
static void generate()
{
auto print = [](const std::string& title, const t_rng& rng) {
std::cout << title << "\n";
for (const auto& [label, num] : rng)
{
std::cout << label << ": " << num << "\n";
}
};
print("\nRNG (Botan):\n", Random::botan_generate());
print("\nRNG (Crypto++):\n", Random::cryptopp_generate());
print("\nRNG (libsodium):\n", Random::libsodium_generate());
std::cout << std::endl;
}
//! \brief Wrapper to Random generator
static void run() { Random::generate(); }
};
} // namespace crypto
} // namespace macro
} // namespace docker_finance
#endif // CONTAINER_SRC_ROOT_MACRO_CRYPTO_RANDOM_C_
// # vim: sw=2 sts=2 si ai et