Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions lib/src/Utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1272,24 +1272,20 @@ std::string secureRandomString(size_t size)

// batch up to 32 bytes of random data for efficiency. Calling
// secureRandomBytes can be expensive.
auto randByte = []() {
thread_local trantor::utils::Hash256 hash;
thread_local size_t i = 0;
if (i == 0)
trantor::utils::Hash256 hash;
for (size_t i = 0; i < size; ++i)
{
auto j = i % sizeof(hash);
if (j == 0)
{
bool ok = trantor::utils::secureRandomBytes(&hash, sizeof(hash));
if (!ok)
throw std::runtime_error(
"Failed to generate random bytes for secureRandomString");
}
unsigned char *hashBytes = reinterpret_cast<unsigned char *>(&hash);
auto ret = hashBytes[i];
i = (i + 1) % sizeof(hash);
return ret;
};

for (size_t i = 0; i < size; ++i)
ret[i] = chars[randByte() % 64];
ret[i] = chars[hashBytes[j] % 64];
}
return ret;
}

Expand Down
Loading