diff --git a/container/src/root/macro/crypto/hash.C b/container/src/root/macro/crypto/hash.C new file mode 100644 index 0000000..c7b8a1d --- /dev/null +++ b/container/src/root/macro/crypto/hash.C @@ -0,0 +1,255 @@ +// 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 . + +//! \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_HASH_C_ +#define CONTAINER_SRC_ROOT_MACRO_CRYPTO_HASH_C_ + +#include +#include +#include + +#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 Hash generator macro +class Hash final +{ + using t_hash = std::map; + + public: + Hash() = default; + ~Hash() = default; + + Hash(const Hash&) = delete; + Hash& operator=(const Hash&) = delete; + + Hash(Hash&&) = default; + Hash& operator=(Hash&&) = default; + + protected: + //! \brief Botan Hash encoder + //! \param message Message to encode + //! \return t_hash Hash map to print (label, digest) + static t_hash botan_encode(const std::string& message) + { + t_hash hash; + + // + // BLAKE2 + // + + hash["botan::Hash::BLAKE2b"] = + botan::g_Hash->encode(message); + + // + // Keccak + // + + hash["botan::Hash::Keccak_224"] = + botan::g_Hash->encode(message); + hash["botan::Hash::Keccak_256"] = + botan::g_Hash->encode(message); + hash["botan::Hash::Keccak_384"] = + botan::g_Hash->encode(message); + hash["botan::Hash::Keccak_512"] = + botan::g_Hash->encode(message); + + // + // SHA + // + + hash["botan::Hash::SHA1"] = + botan::g_Hash->encode(message); + + hash["botan::Hash::SHA2_224"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHA2_256"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHA2_384"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHA2_512"] = + botan::g_Hash->encode(message); + + hash["botan::Hash::SHA3_224"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHA3_256"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHA3_384"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHA3_512"] = + botan::g_Hash->encode(message); + + // + // SHAKE + // + + hash["botan::Hash::SHAKE128"] = + botan::g_Hash->encode(message); + hash["botan::Hash::SHAKE256"] = + botan::g_Hash->encode(message); + + return hash; + } + + //! \brief Crypto++ Hash encoder + //! \param message Message to encode + //! \return t_hash Hash map to print (label, digest) + static t_hash cryptopp_encode(const std::string& message) + { + t_hash hash; + + // + // BLAKE2 + // + + hash["cryptopp::Hash::BLAKE2b"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::BLAKE2s"] = + cryptopp::g_Hash->encode(message); + + // + // Keccak + // + + hash["cryptopp::Hash::Keccak_224"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::Keccak_256"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::Keccak_384"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::Keccak_512"] = + cryptopp::g_Hash->encode(message); + + // + // SHA + // + + hash["cryptopp::Hash::SHA1"] = + cryptopp::g_Hash->encode(message); + + hash["cryptopp::Hash::SHA2_224"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHA2_256"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHA2_384"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHA2_512"] = + cryptopp::g_Hash->encode(message); + + hash["cryptopp::Hash::SHA3_224"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHA3_256"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHA3_384"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHA3_512"] = + cryptopp::g_Hash->encode(message); + + // + // SHAKE + // + + hash["cryptopp::Hash::SHAKE128"] = + cryptopp::g_Hash->encode(message); + hash["cryptopp::Hash::SHAKE256"] = + cryptopp::g_Hash->encode(message); + + return hash; + } + + //! \brief libsodium Hash encoder + //! \param message Message to encode + //! \return t_hash Hash map to print (label, digest) + static t_hash libsodium_encode(const std::string& message) + { + t_hash hash; + + // + // BLAKE2 + // + + hash["libsodium::Hash::BLAKE2b"] = + libsodium::g_Hash->encode(message); + + // + // SHA + // + + hash["libsodium::Hash::SHA2_256"] = + libsodium::g_Hash->encode(message); + hash["libsodium::Hash::SHA2_512"] = + libsodium::g_Hash->encode(message); + + return hash; + } + + public: + //! \brief Print t_hash encoded digest of given message + static void encode(const std::string& message) + { + auto print = [](const std::string& title, const t_hash& hash) { + std::cout << title; + + for (const auto& [label, digest] : hash) + { + std::cout << "\n" << label << "\n" << digest << "\n"; + } + }; + + std::cout << "\nMessage:\n\n" << message << "\n"; + + print("\nEncoded (Botan):\n", Hash::botan_encode(message)); + print("\nEncoded (Crypto++):\n", Hash::cryptopp_encode(message)); + print("\nEncoded (libsodium):\n", Hash::libsodium_encode(message)); + + std::cout << std::endl; + } + + //! \brief Wrapper to encoder + //! \param message Message to encode + static void run(const std::string& message) { Hash::encode(message); } +}; +} // namespace crypto +} // namespace macro +} // namespace docker_finance + +#endif // CONTAINER_SRC_ROOT_MACRO_CRYPTO_HASH_C_ + +// # vim: sw=2 sts=2 si ai et