Adds the sdi_toolBox library (temporary version)
This commit is contained in:
78
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/crypto/digitalHash.h
Normal file
78
sdi_toolBox_1.0.x/toolBox/sdi_toolBox/crypto/digitalHash.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
{{copyright}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{version}}
|
||||
*/
|
||||
|
||||
/*
|
||||
{{license}}
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <openssl/evp.h>
|
||||
#include <span>
|
||||
#include <string>
|
||||
|
||||
namespace sdi_toolBox::crypto
|
||||
{
|
||||
//--------------------------------------------------------------
|
||||
class DigitalHash
|
||||
{
|
||||
public:
|
||||
DigitalHash() = default; // Default constructor
|
||||
virtual ~DigitalHash() = default; // Default destructor
|
||||
DigitalHash(const DigitalHash &obj) = delete; // Copy constructor
|
||||
DigitalHash(DigitalHash &&obj) noexcept = delete; // Move constructor
|
||||
DigitalHash &operator=(const DigitalHash &obj) = delete; // Copy assignment operator
|
||||
DigitalHash &operator=(DigitalHash &&obj) noexcept = delete; // Move assignment operator
|
||||
|
||||
static std::string calculateDataHash(const std::span<const uint8_t> &data, const EVP_MD *md_type); // Calculate digital hash from data buffer
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
/* Calculate digital hash from data buffer */
|
||||
inline std::string DigitalHash::calculateDataHash(const std::span<const uint8_t> &data, const EVP_MD *md_type)
|
||||
{
|
||||
// Initialize OpenSSL digest context
|
||||
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
|
||||
if (!mdctx)
|
||||
throw std::runtime_error("Error: Failed to create EVP_MD_CTX");
|
||||
|
||||
// Initialize digest operation
|
||||
if (EVP_DigestInit_ex(mdctx, md_type, nullptr) != 1)
|
||||
{
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("Error: EVP_DigestInit_ex failed");
|
||||
}
|
||||
|
||||
// Update digest with data
|
||||
if (EVP_DigestUpdate(mdctx, data.data(), data.size()) != 1)
|
||||
{
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("Error: EVP_DigestUpdate failed");
|
||||
}
|
||||
|
||||
// Finalize digest computation
|
||||
std::vector<uint8_t> hash(EVP_MD_size(md_type));
|
||||
unsigned int hash_len = 0;
|
||||
if (EVP_DigestFinal_ex(mdctx, hash.data(), &hash_len) != 1)
|
||||
{
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
throw std::runtime_error("Error: EVP_DigestFinal_ex failed");
|
||||
}
|
||||
|
||||
// Clean up
|
||||
EVP_MD_CTX_free(mdctx);
|
||||
|
||||
// Convert hash to hexadecimal string
|
||||
std::string hash_hex;
|
||||
for (unsigned int i = 0; i < hash_len; ++i)
|
||||
hash_hex += std::format("{:02x}", hash[i]);
|
||||
|
||||
return hash_hex;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
} // namespace sdi_toolBox::crypto
|
||||
Reference in New Issue
Block a user