forked from EvergreenCrypto/docker-finance
root: macro: crypto: add hash.C
Generates encoded digests using all available cryptographic libraries.
This commit is contained in:
255
container/src/root/macro/crypto/hash.C
Normal file
255
container/src/root/macro/crypto/hash.C
Normal file
@@ -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 <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_HASH_C_
|
||||
#define CONTAINER_SRC_ROOT_MACRO_CRYPTO_HASH_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 Hash generator macro
|
||||
class Hash final
|
||||
{
|
||||
using t_hash = std::map<std::string, std::string>;
|
||||
|
||||
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<botan::Hash::BLAKE2b>(message);
|
||||
|
||||
//
|
||||
// Keccak
|
||||
//
|
||||
|
||||
hash["botan::Hash::Keccak_224"] =
|
||||
botan::g_Hash->encode<botan::Hash::Keccak_224>(message);
|
||||
hash["botan::Hash::Keccak_256"] =
|
||||
botan::g_Hash->encode<botan::Hash::Keccak_256>(message);
|
||||
hash["botan::Hash::Keccak_384"] =
|
||||
botan::g_Hash->encode<botan::Hash::Keccak_384>(message);
|
||||
hash["botan::Hash::Keccak_512"] =
|
||||
botan::g_Hash->encode<botan::Hash::Keccak_512>(message);
|
||||
|
||||
//
|
||||
// SHA
|
||||
//
|
||||
|
||||
hash["botan::Hash::SHA1"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA1>(message);
|
||||
|
||||
hash["botan::Hash::SHA2_224"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA2_224>(message);
|
||||
hash["botan::Hash::SHA2_256"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA2_256>(message);
|
||||
hash["botan::Hash::SHA2_384"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA2_384>(message);
|
||||
hash["botan::Hash::SHA2_512"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA2_512>(message);
|
||||
|
||||
hash["botan::Hash::SHA3_224"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA3_224>(message);
|
||||
hash["botan::Hash::SHA3_256"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA3_256>(message);
|
||||
hash["botan::Hash::SHA3_384"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA3_384>(message);
|
||||
hash["botan::Hash::SHA3_512"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHA3_512>(message);
|
||||
|
||||
//
|
||||
// SHAKE
|
||||
//
|
||||
|
||||
hash["botan::Hash::SHAKE128"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHAKE128>(message);
|
||||
hash["botan::Hash::SHAKE256"] =
|
||||
botan::g_Hash->encode<botan::Hash::SHAKE256>(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<cryptopp::Hash::BLAKE2b>(message);
|
||||
hash["cryptopp::Hash::BLAKE2s"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::BLAKE2s>(message);
|
||||
|
||||
//
|
||||
// Keccak
|
||||
//
|
||||
|
||||
hash["cryptopp::Hash::Keccak_224"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::Keccak_224>(message);
|
||||
hash["cryptopp::Hash::Keccak_256"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::Keccak_256>(message);
|
||||
hash["cryptopp::Hash::Keccak_384"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::Keccak_384>(message);
|
||||
hash["cryptopp::Hash::Keccak_512"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::Keccak_512>(message);
|
||||
|
||||
//
|
||||
// SHA
|
||||
//
|
||||
|
||||
hash["cryptopp::Hash::SHA1"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA1>(message);
|
||||
|
||||
hash["cryptopp::Hash::SHA2_224"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA2_224>(message);
|
||||
hash["cryptopp::Hash::SHA2_256"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA2_256>(message);
|
||||
hash["cryptopp::Hash::SHA2_384"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA2_384>(message);
|
||||
hash["cryptopp::Hash::SHA2_512"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA2_512>(message);
|
||||
|
||||
hash["cryptopp::Hash::SHA3_224"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA3_224>(message);
|
||||
hash["cryptopp::Hash::SHA3_256"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA3_256>(message);
|
||||
hash["cryptopp::Hash::SHA3_384"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA3_384>(message);
|
||||
hash["cryptopp::Hash::SHA3_512"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHA3_512>(message);
|
||||
|
||||
//
|
||||
// SHAKE
|
||||
//
|
||||
|
||||
hash["cryptopp::Hash::SHAKE128"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHAKE128>(message);
|
||||
hash["cryptopp::Hash::SHAKE256"] =
|
||||
cryptopp::g_Hash->encode<cryptopp::Hash::SHAKE256>(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<libsodium::Hash::BLAKE2b>(message);
|
||||
|
||||
//
|
||||
// SHA
|
||||
//
|
||||
|
||||
hash["libsodium::Hash::SHA2_256"] =
|
||||
libsodium::g_Hash->encode<libsodium::Hash::SHA2_256>(message);
|
||||
hash["libsodium::Hash::SHA2_512"] =
|
||||
libsodium::g_Hash->encode<libsodium::Hash::SHA2_512>(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
|
||||
Reference in New Issue
Block a user