feat: upgrade session keys to use mersenne twister (#1155)

* upgrade session keys to use mersenne twister

* arithmetic type static assert and windows min/max macro undef
This commit is contained in:
avery 2023-07-23 14:59:43 -07:00 committed by GitHub
parent c791d1a237
commit bd5ead40f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 24 deletions

View File

@ -29,6 +29,7 @@ namespace Game {
dServer* server = nullptr; dServer* server = nullptr;
dConfig* config = nullptr; dConfig* config = nullptr;
bool shouldShutdown = false; bool shouldShutdown = false;
std::mt19937 randomEngine;
} }
dLogger* SetupLogger(); dLogger* SetupLogger();
@ -83,6 +84,8 @@ int main(int argc, char** argv) {
delete res; delete res;
delete stmt; delete stmt;
Game::randomEngine = std::mt19937(time(0));
//It's safe to pass 'localhost' here, as the IP is only used as the external IP. //It's safe to pass 'localhost' here, as the IP is only used as the external IP.
uint32_t maxClients = 50; uint32_t maxClients = 50;
uint32_t ourPort = 1001; //LU client is hardcoded to use this for auth port, so I'm making it the default. uint32_t ourPort = 1001; //LU client is hardcoded to use this for auth port, so I'm making it the default.

View File

@ -111,29 +111,6 @@ namespace GeneralUtils {
*/ */
bool CheckBit(int64_t value, uint32_t index); bool CheckBit(int64_t value, uint32_t index);
// MARK: Random Number Generation
//! Generates a random number
/*!
\param min The minimum the generate from
\param max The maximum to generate to
*/
template <typename T>
inline T GenerateRandomNumber(std::size_t min, std::size_t max) {
// Make sure it is a numeric type
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
if constexpr (std::is_integral_v<T>) { // constexpr only necessary on first statement
std::uniform_int_distribution<T> distribution(min, max);
return distribution(Game::randomEngine);
} else if (std::is_floating_point_v<T>) {
std::uniform_real_distribution<T> distribution(min, max);
return distribution(Game::randomEngine);
}
return T();
}
bool ReplaceInString(std::string& str, const std::string& from, const std::string& to); bool ReplaceInString(std::string& str, const std::string& from, const std::string& to);
std::u16string ReadWString(RakNet::BitStream* inStream); std::u16string ReadWString(RakNet::BitStream* inStream);
@ -223,4 +200,42 @@ namespace GeneralUtils {
std::hash<T> h; std::hash<T> h;
s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2); s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2);
} }
// MARK: Random Number Generation
//! Generates a random number
/*!
\param min The minimum the generate from
\param max The maximum to generate to
*/
template <typename T>
inline T GenerateRandomNumber(std::size_t min, std::size_t max) {
// Make sure it is a numeric type
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
if constexpr (std::is_integral_v<T>) { // constexpr only necessary on first statement
std::uniform_int_distribution<T> distribution(min, max);
return distribution(Game::randomEngine);
} else if (std::is_floating_point_v<T>) {
std::uniform_real_distribution<T> distribution(min, max);
return distribution(Game::randomEngine);
}
return T();
}
// on Windows we need to undef these or else they conflict with our numeric limits calls
// DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS
#ifdef _WIN32
#undef min
#undef max
#endif
template <typename T>
inline T GenerateRandomNumber() {
// Make sure it is a numeric type
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
return GenerateRandomNumber<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
}
} }

View File

@ -8,6 +8,7 @@
#include "ZoneInstanceManager.h" #include "ZoneInstanceManager.h"
#include "MD5.h" #include "MD5.h"
#include "SHA512.h" #include "SHA512.h"
#include "GeneralUtils.h"
#ifdef _WIN32 #ifdef _WIN32
#include <bcrypt/BCrypt.hpp> #include <bcrypt/BCrypt.hpp>
@ -211,7 +212,7 @@ void AuthPackets::SendLoginResponse(dServer* server, const SystemAddress& sysAdd
packet.Write(static_cast<uint16_t>(64)); // Version Minor packet.Write(static_cast<uint16_t>(64)); // Version Minor
// Writes the user key // Writes the user key
uint32_t sessionKey = rand(); // not mt but whatever uint32_t sessionKey = GeneralUtils::GenerateRandomNumber<uint32_t>();
std::string userHash = std::to_string(sessionKey); std::string userHash = std::to_string(sessionKey);
userHash = md5(userHash); userHash = md5(userHash);
PacketUtils::WritePacketWString(userHash, 33, &packet); PacketUtils::WritePacketWString(userHash, 33, &packet);