1#pragma comment(lib, "libcrypto.lib")
5#include <unordered_map>
9#include <openssl/evp.h>
15 std::ifstream file(filename, std::ios::binary);
16 if (!file.is_open()) {
17 Log::GetLog()->error(
"Error opening file for SHA-256 calculation: " + filename.string());
21 const auto fileSize = std::filesystem::file_size(filename);
22 std::vector<
char> buffer(fileSize);
24 if (!file.read(buffer.data(), fileSize)) {
25 Log::GetLog()->error(
"Error reading file for SHA-256 calculation: " + filename.string());
29 std::unique_ptr<EVP_MD_CTX,
decltype(&EVP_MD_CTX_free)> mdctx(EVP_MD_CTX_new(), &EVP_MD_CTX_free);
30 if (mdctx ==
nullptr) {
31 Log::GetLog()->error(
"Error creating EVP_MD_CTX");
35 if (EVP_DigestInit_ex(mdctx.get(), EVP_sha256(),
nullptr) != 1) {
36 Log::GetLog()->error(
"Error initializing SHA-256 context");
40 if (EVP_DigestUpdate(mdctx.get(), buffer.data(), fileSize) != 1) {
41 Log::GetLog()->error(
"Error updating SHA-256 context");
45 unsigned char digest[EVP_MAX_MD_SIZE];
46 unsigned int digestLen;
47 if (EVP_DigestFinal_ex(mdctx.get(), digest, &digestLen) != 1) {
48 Log::GetLog()->error(
"Error finalizing SHA-256 context");
53 result.reserve(digestLen * 2);
54 for (
unsigned int i = 0; i < digestLen; i++) {
56 snprintf(hex,
sizeof(hex),
"%02x", digest[i]);
63 void saveToFile(
const std::filesystem::path& filename,
const std::string& content)
65 std::ofstream file(filename, std::ios::binary | std::ios::trunc);
67 file.write(content.data(), content.size());
72 Log::GetLog()->error(
"Error opening file for writing: " + filename.string());
77 std::ifstream file(filename, std::ios::binary);
80 file.seekg(0, std::ios::end);
81 content.resize(file.tellg());
82 file.seekg(0, std::ios::beg);
83 file.read(&content[0], content.size());
87 if (std::filesystem::exists(filename))
88 Log::GetLog()->error(
"Error file exists but is not readable: " + filename.string());
93 void saveToFilePlain(
const std::filesystem::path& filename,
const std::unordered_map<std::string, intptr_t>& map)
95 std::ofstream file(filename, std::ios::trunc);
98 std::vector<std::pair<std::string, intptr_t>> sortedVec(map.begin(), map.end());
99 std::sort(sortedVec.begin(), sortedVec.end(), [](
const auto& a,
const auto& b) {
return a.first < b.first; });
101 for (
const auto& pair : sortedVec)
103 std::string data(fmt::format(
"{}\n", pair.first));
104 file.write(data.data(), data.size());
110 Log::GetLog()->error(
"Error opening file for writing: " + filename.string());
115 std::unordered_set<std::string> set;
117 std::ifstream file(filename);
119 return default_filters;
122 while (std::getline(file, line))
std::string calculateSHA256(const std::filesystem::path &filename)
void saveToFile(const std::filesystem::path &filename, const std::string &content)
std::string readFromFile(const std::filesystem::path &filename)
void saveToFilePlain(const std::filesystem::path &filename, const std::unordered_map< std::string, intptr_t > &map)
std::unordered_set< std::string > readFileIntoSet(const std::filesystem::path &filename)