summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-01-19 16:35:11 +0000
committerrubidium <rubidium@openttd.org>2011-01-19 16:35:11 +0000
commite95718bf43a7d34868ee11002108ff0dd6e1ab2a (patch)
treed0ea41124e16661fb723515fafb1539bfb2b14d1
parentc9609bbbc2d6b7b2d24869ac0076ad3d4f43ff3f (diff)
downloadopenttd-e95718bf43a7d34868ee11002108ff0dd6e1ab2a.tar.xz
(svn r21852) -Codechange: generalise GenerateCompanyPasswordHash (dihedral)
-rw-r--r--src/network/network.cpp24
-rw-r--r--src/network/network.h2
-rw-r--r--src/network/network_client.cpp8
3 files changed, 18 insertions, 16 deletions
diff --git a/src/network/network.cpp b/src/network/network.cpp
index 80b285235..e6fff9420 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -150,9 +150,6 @@ byte NetworkSpectatorCount()
return count;
}
-extern uint32 _password_game_seed;
-extern char _password_server_id[NETWORK_SERVER_ID_LENGTH];
-
/**
* Sets/resets company password
* @param password new password, "" or "*" resets password
@@ -172,11 +169,13 @@ const char *NetworkChangeCompanyPassword(const char *password)
}
/**
- * Generates a hashed password for the company name.
- * @param password the password to 'encrypt'.
- * @return the hashed password.
+ * Hash the given password using server ID and game seed.
+ * @param password Password to hash.
+ * @param password_server_id Server ID.
+ * @param password_game_seed Game seed.
+ * @return The hashed password.
*/
-const char *GenerateCompanyPasswordHash(const char *password)
+const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
{
if (StrEmpty(password)) return password;
@@ -186,7 +185,7 @@ const char *GenerateCompanyPasswordHash(const char *password)
snprintf(salted_password, sizeof(salted_password), "%s", password);
/* Add the game seed and the server's ID as the salt. */
for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
- salted_password[i] ^= _password_server_id[i] ^ (_password_game_seed >> (i % 32));
+ salted_password[i] ^= password_server_id[i] ^ (password_game_seed >> (i % 32));
}
Md5 checksum;
@@ -208,10 +207,13 @@ const char *GenerateCompanyPasswordHash(const char *password)
*/
void HashCurrentCompanyPassword(const char *password)
{
- _password_game_seed = _settings_game.game_creation.generation_seed;
- strecpy(_password_server_id, _settings_client.network.network_id, lastof(_password_server_id));
+ uint32 password_game_seed;
+ char password_server_id[NETWORK_SERVER_ID_LENGTH];
+
+ password_game_seed = _settings_game.game_creation.generation_seed;
+ strecpy(password_server_id, _settings_client.network.network_id, lastof(password_server_id));
- const char *new_pw = GenerateCompanyPasswordHash(password);
+ const char *new_pw = GenerateCompanyPasswordHash(password, password_server_id, password_game_seed);
strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password));
if (_network_server) {
diff --git a/src/network/network.h b/src/network/network.h
index 4874d4f8a..098605111 100644
--- a/src/network/network.h
+++ b/src/network/network.h
@@ -19,7 +19,7 @@ void NetworkStartUp();
void NetworkShutDown();
void NetworkDrawChatMessage();
-const char *GenerateCompanyPasswordHash(const char *password);
+const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed);
void HashCurrentCompanyPassword(const char *password);
extern bool _networking; ///< are we in networking mode?
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index 4b14ac3af..0f6606352 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -347,7 +347,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const char *p
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const char *password)
{
Packet *p = new Packet(PACKET_CLIENT_COMPANY_PASSWORD);
- p->Send_string(GenerateCompanyPasswordHash(password));
+ p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
my_client->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;
}
@@ -426,7 +426,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const char *pa
{
Packet *p = new Packet(PACKET_CLIENT_SET_PASSWORD);
- p->Send_string(GenerateCompanyPasswordHash(password));
+ p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
my_client->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;
}
@@ -457,11 +457,11 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendRCon(const char *pass, con
return NETWORK_RECV_STATUS_OKAY;
}
-NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const char *pass)
+NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const char *password)
{
Packet *p = new Packet(PACKET_CLIENT_MOVE);
p->Send_uint8(company);
- p->Send_string(GenerateCompanyPasswordHash(pass));
+ p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
my_client->SendPacket(p);
return NETWORK_RECV_STATUS_OKAY;
}