2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-17 18:15:09 +00:00
qpdf/libqpdf/InsecureRandomDataProvider.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

48 lines
1.1 KiB
C++
Raw Normal View History

#include <qpdf/InsecureRandomDataProvider.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/qpdf-config.h>
2023-05-20 11:22:32 +00:00
#include <cstdlib>
InsecureRandomDataProvider::InsecureRandomDataProvider() :
seeded_random(false)
{
}
void
InsecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
{
for (size_t i = 0; i < len; ++i) {
data[i] = static_cast<unsigned char>((this->random() & 0xff0) >> 4);
}
}
long
InsecureRandomDataProvider::random()
{
if (!this->seeded_random) {
// Seed the random number generator with something simple, but just to be interesting, don't
// use the unmodified current time. It would be better if this were a more secure seed.
2020-04-06 13:49:02 +00:00
auto seed = static_cast<unsigned int>(QUtil::get_current_time() ^ 0xcccc);
#ifdef HAVE_RANDOM
::srandom(seed);
#else
srand(seed);
#endif
this->seeded_random = true;
}
#ifdef HAVE_RANDOM
return ::random();
#else
return rand();
#endif
}
RandomDataProvider*
InsecureRandomDataProvider::getInstance()
{
static InsecureRandomDataProvider instance;
return &instance;
}