Optimize rng (ddos protection)

This commit is contained in:
momo5502 2016-02-18 21:33:19 +01:00
parent d8a4c91ff1
commit d4c7c0fb6f
3 changed files with 45 additions and 8 deletions

View File

@ -74,9 +74,7 @@ namespace Components
auto startTime = std::chrono::high_resolution_clock::now(); auto startTime = std::chrono::high_resolution_clock::now();
bool testRes = component->UnitTest(); bool testRes = component->UnitTest();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startTime).count(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startTime).count();
Logger::Print("Test done (%llims): %s\n\n", duration, (testRes ? "Success" : "Error"));
Logger::Print("Test done (%llims): %s\n\n", duration, (testRes ? "Success" : "Error"));//
result &= testRes; result &= testRes;
} }

View File

@ -766,6 +766,20 @@ namespace Components
} }
printf("Success\n"); printf("Success\n");
uint32_t randIntCount = 4'000'000;
printf("Generating %d random integers...", randIntCount);
auto startTime = std::chrono::high_resolution_clock::now();
for (uint32_t i = 0; i < randIntCount; ++i)
{
Utils::Cryptography::Rand::GenerateInt();
}
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startTime).count();
Logger::Print("took %llims\n", duration);
return true; return true;
} }
} }

View File

@ -10,15 +10,40 @@ namespace Utils
uint32_t Rand::GenerateInt() uint32_t Rand::GenerateInt()
{ {
size_t length = 0; static int length = 0;
uint8_t buffer[4] = { 0 }; static int time = 0;
static int access = 0;
static uint8_t randPool[2048] = { 0 };
while (length != 4) int mseconds = Game::Com_Milliseconds();
if ((mseconds - time) > (1000 * 60 * 5) || (access > (sizeof(randPool) * 30)))
{ {
length = sprng_read(buffer, sizeof(buffer), NULL); access = 0;
length = 0;
} }
return *reinterpret_cast<uint32_t*>(buffer); while (length != sizeof(randPool))
{
length += sprng_read(randPool, sizeof(randPool), NULL);
}
uint8_t numberBuf[4] = { 0 };
for (int i = 0; i < sizeof(numberBuf); ++i)
{
numberBuf[i] = randPool[(rand() + mseconds + i + numberBuf[(i > 0 ? (i - 1) : 0)]) % sizeof(randPool)];
}
uint32_t num = *reinterpret_cast<uint32_t*>(numberBuf);
if (!(access % 100))
{
srand(num);
}
access++;
return num;
} }
#pragma endregion #pragma endregion