mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-16 16:55:27 +00:00
fix: generate session keys from the system entropy source
The key came from the shared game random engine, which is seeded from the start time. It now comes from std::random_device through a small helper. The key stays a 32 bit number, so the packets do not change. The master and world servers no longer write the key or its hash to the log. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -372,6 +372,23 @@ namespace GeneralUtils {
|
||||
return GenerateRandomNumber<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a number from the operating system's entropy source instead of the shared game engine.
|
||||
* Use this for values that must not be predictable from earlier ones, such as session keys.
|
||||
*/
|
||||
template <typename T>
|
||||
inline T GenerateUnpredictableNumber() {
|
||||
static_assert(std::is_integral_v<T> && std::is_unsigned_v<T>, "Not an unsigned integral type");
|
||||
|
||||
std::random_device device;
|
||||
T value = 0;
|
||||
for (uint32_t bytesFilled = 0; bytesFilled < sizeof(T); bytesFilled += sizeof(uint32_t)) {
|
||||
// Two half shifts, a full 32 bit shift is not valid on a 32 bit type
|
||||
value = static_cast<T>(static_cast<T>(value << 16) << 16) | static_cast<T>(device());
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// https://www.quora.com/How-do-you-round-to-specific-increments-like-0-5-in-C
|
||||
// Rounds to the nearest floating point value specified.
|
||||
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
|
||||
Reference in New Issue
Block a user