2013-11-30 17:02:56 +00:00
|
|
|
#include <qpdf/InsecureRandomDataProvider.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/qpdf-config.h>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdlib>
|
2013-11-30 17:02:56 +00:00
|
|
|
|
|
|
|
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) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// 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
|
2022-02-08 14:18:08 +00:00
|
|
|
this->seeded_random = true;
|
2013-11-30 17:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_RANDOM
|
|
|
|
return ::random();
|
|
|
|
#else
|
|
|
|
return rand();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomDataProvider*
|
|
|
|
InsecureRandomDataProvider::getInstance()
|
|
|
|
{
|
|
|
|
static InsecureRandomDataProvider instance;
|
|
|
|
return &instance;
|
|
|
|
}
|