2013-11-30 17:02:56 +00:00
|
|
|
#include <qpdf/InsecureRandomDataProvider.hh>
|
|
|
|
|
|
|
|
#include <qpdf/qpdf-config.h>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
InsecureRandomDataProvider::InsecureRandomDataProvider() :
|
|
|
|
seeded_random(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InsecureRandomDataProvider::~InsecureRandomDataProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2019-06-21 03:35:23 +00:00
|
|
|
QUtil::srandom(static_cast<unsigned int>(
|
|
|
|
QUtil::get_current_time() ^ 0xcccc));
|
2013-11-30 17:02:56 +00:00
|
|
|
this->seeded_random = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef HAVE_RANDOM
|
|
|
|
return ::random();
|
|
|
|
# else
|
|
|
|
return rand();
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomDataProvider*
|
|
|
|
InsecureRandomDataProvider::getInstance()
|
|
|
|
{
|
|
|
|
static InsecureRandomDataProvider instance;
|
|
|
|
return &instance;
|
|
|
|
}
|