2016-07-13 22:45:38 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-07-13 22:45:38 +00:00
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include "Common/MD5.h"
|
|
|
|
|
2016-07-13 22:45:38 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <functional>
|
|
|
|
#include <mbedtls/md5.h>
|
|
|
|
#include <string>
|
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2016-07-13 22:45:38 +00:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "DiscIO/Blob.h"
|
|
|
|
|
|
|
|
namespace MD5
|
|
|
|
{
|
|
|
|
std::string MD5Sum(const std::string& file_path, std::function<bool(int)> report_progress)
|
|
|
|
{
|
|
|
|
std::string output_string;
|
|
|
|
std::vector<u8> data(8 * 1024 * 1024);
|
|
|
|
u64 read_offset = 0;
|
|
|
|
mbedtls_md5_context ctx;
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
std::unique_ptr<DiscIO::BlobReader> file(DiscIO::CreateBlobReader(file_path));
|
2016-07-13 22:45:38 +00:00
|
|
|
u64 game_size = file->GetDataSize();
|
|
|
|
|
2019-06-08 00:57:02 +00:00
|
|
|
mbedtls_md5_starts_ret(&ctx);
|
2016-07-13 22:45:38 +00:00
|
|
|
|
|
|
|
while (read_offset < game_size)
|
|
|
|
{
|
|
|
|
size_t read_size = std::min(static_cast<u64>(data.size()), game_size - read_offset);
|
|
|
|
if (!file->Read(read_offset, read_size, data.data()))
|
|
|
|
return output_string;
|
|
|
|
|
2019-06-08 00:57:02 +00:00
|
|
|
mbedtls_md5_update_ret(&ctx, data.data(), read_size);
|
2016-07-13 22:45:38 +00:00
|
|
|
read_offset += read_size;
|
|
|
|
|
|
|
|
int progress =
|
|
|
|
static_cast<int>(static_cast<float>(read_offset) / static_cast<float>(game_size) * 100);
|
|
|
|
if (!report_progress(progress))
|
|
|
|
return output_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<u8, 16> output;
|
2019-06-08 00:57:02 +00:00
|
|
|
mbedtls_md5_finish_ret(&ctx, output.data());
|
2016-07-13 22:45:38 +00:00
|
|
|
|
|
|
|
// Convert to hex
|
|
|
|
for (u8 n : output)
|
2019-06-14 14:53:46 +00:00
|
|
|
output_string += fmt::format("{:02x}", n);
|
2016-07-13 22:45:38 +00:00
|
|
|
|
|
|
|
return output_string;
|
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace MD5
|