MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / CreateMd5FileChecksum

Function CreateMd5FileChecksum

lib/mdflib/mdflib/src/cryptoutil.cpp:244–284  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

242namespace mdf {
243
244bool 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
286std::string CreateMd5FileString(const std::string &file) {
287 std::vector<uint8_t> checksum(16, 0);

Callers 3

WriteMethod · 0.85
ReadDataMethod · 0.85
CreateMd5FileStringFunction · 0.85

Calls 7

fileopenFunction · 0.85
MD5_InitFunction · 0.85
MD5_UpdateFunction · 0.85
MD5_FinalFunction · 0.85
sizeMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected