/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Core/Utils/FileUtils.cpp
92 строки
2 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "FileUtils.h" #include <fstream> #include <sstream> #include <iomanip> #include <openssl/evp.h> namespace Core::Utils { namespace { std::string toHexString(const unsigned char* digest, std::size_t length) { std::ostringstream oss; oss << std::hex << std::setfill('0'); for(std::size_t i = 0; i < length; ++i) { oss << std::setw(2) << static_cast<int>(digest[i]); } return oss.str(); } std::string calculateHash(std::ifstream& file, const EVP_MD* (*evpFunc)()) { EVP_MD_CTX* ctx = EVP_MD_CTX_new(); if (!ctx) return ""; if (EVP_DigestInit_ex(ctx, evpFunc(), nullptr) != 1) { EVP_MD_CTX_free(ctx); return ""; } char buffer[8192]; while (file.good()) { file.read(buffer, sizeof(buffer)); std::streamsize bytesRead = file.gcount(); if (bytesRead > 0) { EVP_DigestUpdate(ctx, buffer, bytesRead); } } unsigned char hash[EVP_MAX_MD_SIZE]; unsigned int length = 0; EVP_DigestFinal_ex(ctx, hash, &length); EVP_MD_CTX_free(ctx); return toHexString(hash, length); } } // namespace std::string FileUtils::calculateChecksum(const std::string& filePath, HashAlgorithm algorithm) { std::ifstream file(filePath, std::ios::binary); if(!file.is_open()) { return ""; } switch (algorithm) { case HashAlgorithm::MD5: return calculateMD5(file); case HashAlgorithm::SHA1: return calculateSHA1(file); case HashAlgorithm::SHA256: return calculateSHA256(file); default: return calculateSHA256(file); } } std::string FileUtils::calculateSHA256(std::ifstream& file) { return calculateHash(file, EVP_sha256); } std::string FileUtils::calculateMD5(std::ifstream& file) { return calculateHash(file, EVP_md5); } std::string FileUtils::calculateSHA1(std::ifstream& file) { return calculateHash(file, EVP_sha1); } } // namespace Core::Utils