Optimize rng (ddos protection)
This commit is contained in:
parent
d8a4c91ff1
commit
d4c7c0fb6f
@ -74,9 +74,7 @@ namespace Components
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
bool testRes = component->UnitTest();
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -766,6 +766,20 @@ namespace Components
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -10,15 +10,40 @@ namespace Utils
|
||||
|
||||
uint32_t Rand::GenerateInt()
|
||||
{
|
||||
size_t length = 0;
|
||||
uint8_t buffer[4] = { 0 };
|
||||
static int length = 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
|
||||
|
Loading…
Reference in New Issue
Block a user