From e2dc5aa83e75b2f1f34feaadec3ae8f4e2a484b0 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Sun, 16 May 2021 12:10:12 +0200 Subject: Codechange: [Network] Use C++ string functions to generate company password hash --- src/network/network.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/network') diff --git a/src/network/network.cpp b/src/network/network.cpp index 15e4b8d2a..53ef645d7 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -34,6 +34,8 @@ #include "../gfx_func.h" #include "../error.h" #include +#include +#include #include "../safeguards.h" @@ -176,29 +178,31 @@ std::string GenerateCompanyPasswordHash(const std::string &password, const std:: { if (password.empty()) return password; - char salted_password[NETWORK_SERVER_ID_LENGTH]; size_t password_length = password.size(); size_t password_server_id_length = password_server_id.size(); - /* Add the game seed and the server's ID as the salt. */ + std::ostringstream salted_password; + /* Add the password with the server's ID and game seed as the salt. */ for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) { char password_char = (i < password_length ? password[i] : 0); char server_id_char = (i < password_server_id_length ? password_server_id[i] : 0); char seed_char = password_game_seed >> (i % 32); - salted_password[i] = password_char ^ server_id_char ^ seed_char; + salted_password << (char)(password_char ^ server_id_char ^ seed_char); // Cast needed, otherwise interpreted as integer to format } Md5 checksum; uint8 digest[16]; - static char hashed_password[NETWORK_SERVER_ID_LENGTH]; /* Generate the MD5 hash */ - checksum.Append(salted_password, sizeof(salted_password) - 1); + std::string salted_password_string = salted_password.str(); + checksum.Append(salted_password_string.data(), salted_password_string.size()); checksum.Finish(digest); - for (int di = 0; di < 16; di++) seprintf(hashed_password + di * 2, lastof(hashed_password), "%02x", digest[di]); + std::ostringstream hashed_password; + hashed_password << std::hex << std::setfill('0'); + for (int di = 0; di < 16; di++) hashed_password << std::setw(2) << (int)digest[di]; // Cast needed, otherwise interpreted as character to add - return hashed_password; + return hashed_password.str(); } /** -- cgit v1.2.3-54-g00ecf