Replace BytesToHexString with FormatArrayAsHex

This commit is contained in:
Jonathan G Rennison
2023-11-13 23:26:05 +00:00
parent 6a35661db4
commit beee3cc369
6 changed files with 29 additions and 21 deletions

View File

@@ -36,6 +36,7 @@
#include "../gfx_func.h"
#include "../error.h"
#include "../core/checksum_func.hpp"
#include "../string_func.h"
#include "../string_func_extra.h"
#include "../core/serialisation.hpp"
#include "../3rdparty/randombytes/randombytes.h"
@@ -215,7 +216,7 @@ std::string GenerateCompanyPasswordHash(const std::string &password, const std::
checksum.Append(salted_password_string.data(), salted_password_string.size());
checksum.Finish(digest);
return BytesToHexString(digest.data(), digest.size());
return FormatArrayAsHex(digest);
}
/**
@@ -1340,27 +1341,12 @@ static void NetworkGenerateServerId()
_settings_client.network.network_id = GenerateUid("OpenTTD Server ID");
}
std::string BytesToHexString(const byte *data, size_t length)
{
std::string hex_output;
hex_output.resize(length * 2);
char txt[3];
for (uint i = 0; i < length; ++i) {
seprintf(txt, lastof(txt), "%02x", data[i]);
hex_output[i * 2] = txt[0];
hex_output[(i * 2) + 1] = txt[1];
}
return hex_output;
}
std::string NetworkGenerateRandomKeyString(uint bytes)
{
uint8 *key = AllocaM(uint8, bytes);
NetworkRandomBytesWithFallback(key, bytes);
return BytesToHexString(key, bytes);
return FormatArrayAsHex({key, bytes});
}
class TCPNetworkDebugConnecter : TCPConnecter {

View File

@@ -142,7 +142,6 @@ StringID GetNetworkErrorMsg(NetworkErrorCode err);
bool NetworkMakeClientNameUnique(std::string &new_name);
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed);
std::vector<uint8> GenerateGeneralPasswordHash(const std::string &password, const std::string &password_server_id, uint64 password_game_seed);
std::string BytesToHexString(const byte *data, size_t length);
std::string NetworkGenerateRandomKeyString(uint bytes);
std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id);

View File

@@ -20,6 +20,7 @@
#include "../timer/timer_game_tick.h"
#include "../sl/saveload.h"
#include "../date_func.h"
#include "../string_func.h"
#include "../currency.h"
#include "../fontcache.h"
@@ -237,7 +238,7 @@ static void SurveyGrfs(nlohmann::json &survey)
auto grfid = fmt::format("{:08x}", BSWAP32(c->ident.grfid));
auto &grf = survey[grfid];
grf["md5sum"] = BytesToHexString(c->ident.md5sum.data(), c->ident.md5sum.size());
grf["md5sum"] = FormatArrayAsHex(c->ident.md5sum);
grf["status"] = c->status;
if ((c->palette & GRFP_GRF_MASK) == GRFP_GRF_UNSET) grf["palette"] = "unset";