summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-05-16 12:10:12 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-17 16:09:10 +0200
commite2dc5aa83e75b2f1f34feaadec3ae8f4e2a484b0 (patch)
treecf164fd76d228f973bbf6ef30a41c56a053b096d /src/network
parente2e06633c9ff41260d54691f9e7c10dcfe7395fb (diff)
downloadopenttd-e2dc5aa83e75b2f1f34feaadec3ae8f4e2a484b0.tar.xz
Codechange: [Network] Use C++ string functions to generate company password hash
Diffstat (limited to 'src/network')
-rw-r--r--src/network/network.cpp18
1 files changed, 11 insertions, 7 deletions
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 <charconv>
+#include <sstream>
+#include <iomanip>
#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();
}
/**