Replace BytesToHexString with FormatArrayAsHex

pull/603/merge
Jonathan G Rennison 7 months ago
parent 6a35661db4
commit beee3cc369

@ -40,6 +40,7 @@
#include "cargopacket.h"
#include "tbtr_template_vehicle_func.h"
#include "event_logs.h"
#include "string_func.h"
#include "3rdparty/monocypher/monocypher.h"
#include "safeguards.h"
@ -77,7 +78,6 @@ void InitializeOldNames();
std::string GenerateUid(std::string_view subject)
{
extern void NetworkRandomBytesWithFallback(void *buf, size_t n);
extern std::string BytesToHexString(const byte *data, size_t length);
uint8 random_bytes[32];
NetworkRandomBytesWithFallback(random_bytes, lengthof(random_bytes));
@ -90,7 +90,7 @@ std::string GenerateUid(std::string_view subject)
crypto_blake2b_update(&ctx, (const byte *)subject.data(), subject.size());
crypto_blake2b_final (&ctx, digest);
return BytesToHexString(digest, lengthof(digest));
return FormatArrayAsHex({digest, lengthof(digest)});
}
/**

@ -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 {

@ -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);

@ -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";

@ -191,6 +191,25 @@ const char *str_fix_scc_encoded(char *str, const char *last)
return str;
}
/**
* Format a byte array into a continuous hex string.
* @param data Array to format
* @return Converted string.
*/
std::string FormatArrayAsHex(span<const byte> data)
{
std::string hex_output;
hex_output.resize(data.size() * 2);
char txt[3];
for (uint i = 0; i < data.size(); ++i) {
seprintf(txt, lastof(txt), "%02x", data[i]);
hex_output[i * 2] = txt[0];
hex_output[(i * 2) + 1] = txt[1];
}
return hex_output;
}
/**
* Copies the valid (UTF-8) characters from \c str up to \c last to the \c dst.

@ -28,6 +28,7 @@
#include <iosfwd>
#include "core/bitmath_func.hpp"
#include "core/span_type.hpp"
#include "string_type.h"
char *strecat(char *dst, const char *src, const char *last) NOACCESS(3);
@ -40,6 +41,8 @@ int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
std::string CDECL stdstr_fmt(const char *str, ...) WARN_FORMAT(1, 2);
std::string stdstr_vfmt(const char *str, va_list va) WARN_FORMAT(1, 0);
std::string FormatArrayAsHex(span<const byte> data);
char *StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
[[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);

Loading…
Cancel
Save