mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-16 16:55:27 +00:00
fix: session keys are 16 bytes sent as 32 hex characters
The key was a 32 bit number, sent to the client as the MD5 hash of its decimal text. The client only stores the key and sends it back, so it is now 16 bytes from the system entropy source written as 32 lowercase hex characters, which fills the same 33 character field of the login response. The auth, master and world servers pass the key itself between them as a 33 character string in place of the number: SET_SESSION_KEY, SESSION_KEY_RESPONSE and NEW_SESSION_ALERT keep their IDs and carry the string where the number was. The master server keeps one key for each username, and a world server compares the key a client sends with the one from the master server in constant time. A key stays valid until the next login of its account replaces it, as before. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -369,3 +369,30 @@ namespace GeneralUtils::details {
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
std::string GeneralUtils::GenerateUnpredictableHexString(const uint32_t byteCount) {
|
||||
constexpr std::string_view hexDigits = "0123456789abcdef";
|
||||
|
||||
std::string hex;
|
||||
hex.reserve(static_cast<uint64_t>(byteCount) * 2);
|
||||
uint32_t word = 0;
|
||||
for (uint32_t i = 0; i < byteCount; i++) {
|
||||
// One 32 bit draw gives four bytes
|
||||
if (i % 4 == 0) word = GenerateUnpredictableNumber<uint32_t>();
|
||||
const uint8_t byte = static_cast<uint8_t>(word >> ((i % 4) * 8));
|
||||
hex.push_back(hexDigits[byte >> 4]);
|
||||
hex.push_back(hexDigits[byte & 0x0F]);
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
bool GeneralUtils::EqualsInConstantTime(const std::string_view a, const std::string_view b) {
|
||||
if (a.size() != b.size()) return false;
|
||||
|
||||
// volatile keeps the compiler from leaving the loop at the first difference
|
||||
volatile uint8_t difference = 0;
|
||||
for (uint64_t i = 0; i < a.size(); i++) {
|
||||
difference = difference | (static_cast<uint8_t>(a[i]) ^ static_cast<uint8_t>(b[i]));
|
||||
}
|
||||
return difference == 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user