| 242 | namespace mdf { |
| 243 | |
| 244 | bool CreateMd5FileChecksum(const std::string &file, std::vector<uint8_t> &md5) { |
| 245 | bool ok = false; |
| 246 | if (md5.size() != 16) { |
| 247 | md5.resize(16); |
| 248 | } |
| 249 | |
| 250 | try { |
| 251 | fs::path p = fs::u8path(file); |
| 252 | if (fs::exists(p)) { |
| 253 | std::FILE *f = nullptr; |
| 254 | Platform::fileopen(&f, file.c_str(), "rb"); |
| 255 | if (f != nullptr) { |
| 256 | std::array<uint8_t, 10000> temp{}; |
| 257 | MD5_CTX ctx{}; |
| 258 | MD5_Init(&ctx); |
| 259 | for (auto bytes = |
| 260 | std::fread(temp.data(), sizeof(uint8_t), temp.size(), f); |
| 261 | bytes > 0; |
| 262 | bytes = std::fread(temp.data(), sizeof(uint8_t), temp.size(), f)) { |
| 263 | MD5_Update(&ctx, temp.data(), static_cast<unsigned long>(bytes)); |
| 264 | } |
| 265 | fclose(f); |
| 266 | MD5_Final(md5.data(), &ctx); |
| 267 | ok = true; |
| 268 | } else { |
| 269 | MDF_ERROR() << "Failed to create MD5 file checksum. File: " << file |
| 270 | << ". Error: Failed to open the file"; |
| 271 | } |
| 272 | |
| 273 | } else { |
| 274 | MDF_ERROR() << "Failed to create MD5 file checksum. File: " << file |
| 275 | << ". Error: The file doesn't exist"; |
| 276 | } |
| 277 | |
| 278 | } catch (const std::exception &error) { |
| 279 | MDF_ERROR() << "Failed to create MD5 file checksum. File: " << file |
| 280 | << ". Error: " << error.what(); |
| 281 | ok = false; |
| 282 | } |
| 283 | return ok; |
| 284 | } |
| 285 | |
| 286 | std::string CreateMd5FileString(const std::string &file) { |
| 287 | std::vector<uint8_t> checksum(16, 0); |