1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Use secure randomness to seed internal RNG

This commit is contained in:
sfan5 2025-03-30 16:42:26 +02:00
parent 785c042f1f
commit 1281173e50
4 changed files with 18 additions and 18 deletions

View file

@ -700,12 +700,18 @@ static bool init_common(const Settings &cmd_args, int argc, char *argv[])
init_log_streams(cmd_args);
// Initialize random seed
{
u32 seed = static_cast<u32>(time(nullptr)) << 16;
seed |= porting::getTimeUs() & 0xffff;
srand(seed);
mysrand(seed);
u64 seed;
if (!porting::secure_rand_fill_buf(&seed, sizeof(seed))) {
verbosestream << "Secure randomness not available to seed global RNG." << std::endl;
std::ostringstream oss;
// some stuff that's hard to predict:
oss << time(nullptr) << porting::getTimeUs() << argc << g_settings_path;
print_version(oss);
std::string data = oss.str();
seed = murmur_hash_64_ua(data.c_str(), data.size(), 0xc0ffee);
}
srand(seed);
mysrand(seed);
// Initialize HTTP fetcher
httpfetch_init(g_settings->getS32("curl_parallel_limit"));