2013-11-30 17:02:56 +00:00
|
|
|
#include <qpdf/InsecureRandomDataProvider.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/qpdf-config.h>
|
2013-11-30 17:02:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
InsecureRandomDataProvider::InsecureRandomDataProvider() :
|
|
|
|
seeded_random(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InsecureRandomDataProvider::~InsecureRandomDataProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InsecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2013-11-30 17:02:56 +00:00
|
|
|
data[i] = static_cast<unsigned char>((this->random() & 0xff0) >> 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
InsecureRandomDataProvider::random()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
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.
|
2022-04-02 21:14:10 +00:00
|
|
|
unsigned int seed =
|
|
|
|
static_cast<unsigned int>(QUtil::get_current_time() ^ 0xcccc);
|
2020-04-06 13:49:02 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#ifdef HAVE_RANDOM
|
2013-11-30 17:02:56 +00:00
|
|
|
return ::random();
|
2022-04-02 21:14:10 +00:00
|
|
|
#else
|
2013-11-30 17:02:56 +00:00
|
|
|
return rand();
|
2022-04-02 21:14:10 +00:00
|
|
|
#endif
|
2013-11-30 17:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RandomDataProvider*
|
|
|
|
InsecureRandomDataProvider::getInstance()
|
|
|
|
{
|
|
|
|
static InsecureRandomDataProvider instance;
|
|
|
|
return &instance;
|
|
|
|
}
|