Merge pull request #10905 from shuffle2/netplay-hash

netplay: use sha1 instead of md5
This commit is contained in:
Admiral H. Curtiss
2022-08-22 15:57:49 +02:00
committed by GitHub
11 changed files with 176 additions and 179 deletions

View File

@ -16,11 +16,11 @@
#include <vector>
#include <fmt/format.h>
#include <mbedtls/md5.h>
#include "Common/Assert.h"
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/Crypto/SHA1.h"
#include "Common/ENetUtil.h"
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
@ -90,10 +90,10 @@ NetPlayClient::~NetPlayClient()
if (m_is_connected)
{
m_should_compute_MD5 = false;
m_dialog->AbortMD5();
if (m_MD5_thread.joinable())
m_MD5_thread.join();
m_should_compute_game_digest = false;
m_dialog->AbortGameDigest();
if (m_game_digest_thread.joinable())
m_game_digest_thread.join();
m_do_loop.Clear();
m_thread.join();
@ -444,24 +444,24 @@ void NetPlayClient::OnData(sf::Packet& packet)
OnSyncCodes(packet);
break;
case MessageID::ComputeMD5:
OnComputeMD5(packet);
case MessageID::ComputeGameDigest:
OnComputeGameDigest(packet);
break;
case MessageID::MD5Progress:
OnMD5Progress(packet);
case MessageID::GameDigestProgress:
OnGameDigestProgress(packet);
break;
case MessageID::MD5Result:
OnMD5Result(packet);
case MessageID::GameDigestResult:
OnGameDigestResult(packet);
break;
case MessageID::MD5Error:
OnMD5Error(packet);
case MessageID::GameDigestError:
OnGameDigestError(packet);
break;
case MessageID::MD5Abort:
OnMD5Abort();
case MessageID::GameDigestAbort:
OnGameDigestAbort();
break;
default:
@ -1421,48 +1421,48 @@ void NetPlayClient::OnSyncCodesDataAR(sf::Packet& packet)
ActionReplay::UpdateSyncedCodes(synced_codes);
}
void NetPlayClient::OnComputeMD5(sf::Packet& packet)
void NetPlayClient::OnComputeGameDigest(sf::Packet& packet)
{
SyncIdentifier sync_identifier;
ReceiveSyncIdentifier(packet, sync_identifier);
ComputeMD5(sync_identifier);
ComputeGameDigest(sync_identifier);
}
void NetPlayClient::OnMD5Progress(sf::Packet& packet)
void NetPlayClient::OnGameDigestProgress(sf::Packet& packet)
{
PlayerId pid;
int progress;
packet >> pid;
packet >> progress;
m_dialog->SetMD5Progress(pid, progress);
m_dialog->SetGameDigestProgress(pid, progress);
}
void NetPlayClient::OnMD5Result(sf::Packet& packet)
void NetPlayClient::OnGameDigestResult(sf::Packet& packet)
{
PlayerId pid;
std::string result;
packet >> pid;
packet >> result;
m_dialog->SetMD5Result(pid, result);
m_dialog->SetGameDigestResult(pid, result);
}
void NetPlayClient::OnMD5Error(sf::Packet& packet)
void NetPlayClient::OnGameDigestError(sf::Packet& packet)
{
PlayerId pid;
std::string error;
packet >> pid;
packet >> error;
m_dialog->SetMD5Result(pid, error);
m_dialog->SetGameDigestResult(pid, error);
}
void NetPlayClient::OnMD5Abort()
void NetPlayClient::OnGameDigestAbort()
{
m_should_compute_MD5 = false;
m_dialog->AbortMD5();
m_should_compute_game_digest = false;
m_dialog->AbortGameDigest();
}
void NetPlayClient::Send(const sf::Packet& packet, const u8 channel_id)
@ -2438,16 +2438,15 @@ bool NetPlayClient::DoAllPlayersHaveGame()
});
}
static std::string MD5Sum(const std::string& file_path, std::function<bool(int)> report_progress)
static std::string SHA1Sum(const std::string& file_path, std::function<bool(int)> report_progress)
{
std::vector<u8> data(8 * 1024 * 1024);
u64 read_offset = 0;
mbedtls_md5_context ctx;
std::unique_ptr<DiscIO::BlobReader> file(DiscIO::CreateBlobReader(file_path));
u64 game_size = file->GetDataSize();
mbedtls_md5_starts_ret(&ctx);
auto ctx = Common::SHA1::CreateContext();
while (read_offset < game_size)
{
@ -2455,7 +2454,7 @@ static std::string MD5Sum(const std::string& file_path, std::function<bool(int)>
if (!file->Read(read_offset, read_size, data.data()))
return "";
mbedtls_md5_update_ret(&ctx, data.data(), read_size);
ctx->Update(data.data(), read_size);
read_offset += read_size;
int progress =
@ -2464,20 +2463,17 @@ static std::string MD5Sum(const std::string& file_path, std::function<bool(int)>
return "";
}
std::array<u8, 16> output;
mbedtls_md5_finish_ret(&ctx, output.data());
// Convert to hex
return fmt::format("{:02x}", fmt::join(output, ""));
return fmt::format("{:02x}", fmt::join(ctx->Finish(), ""));
}
void NetPlayClient::ComputeMD5(const SyncIdentifier& sync_identifier)
void NetPlayClient::ComputeGameDigest(const SyncIdentifier& sync_identifier)
{
if (m_should_compute_MD5)
if (m_should_compute_game_digest)
return;
m_dialog->ShowMD5Dialog(sync_identifier.game_id);
m_should_compute_MD5 = true;
m_dialog->ShowGameDigestDialog(sync_identifier.game_id);
m_should_compute_game_digest = true;
std::string file;
if (sync_identifier == GetSDCardIdentifier())
@ -2488,26 +2484,26 @@ void NetPlayClient::ComputeMD5(const SyncIdentifier& sync_identifier)
if (file.empty() || !File::Exists(file))
{
sf::Packet packet;
packet << MessageID::MD5Error;
packet << MessageID::GameDigestError;
packet << "file not found";
Send(packet);
return;
}
if (m_MD5_thread.joinable())
m_MD5_thread.join();
m_MD5_thread = std::thread([this, file]() {
std::string sum = MD5Sum(file, [&](int progress) {
if (m_game_digest_thread.joinable())
m_game_digest_thread.join();
m_game_digest_thread = std::thread([this, file]() {
std::string sum = SHA1Sum(file, [&](int progress) {
sf::Packet packet;
packet << MessageID::MD5Progress;
packet << MessageID::GameDigestProgress;
packet << progress;
SendAsync(std::move(packet));
return m_should_compute_MD5;
return m_should_compute_game_digest;
});
sf::Packet packet;
packet << MessageID::MD5Result;
packet << MessageID::GameDigestResult;
packet << sum;
SendAsync(std::move(packet));
});