2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-18 10:25:12 +00:00
qpdf/libqpdf/InsecureRandomDataProvider.cc
Jay Berkenbilt 5e3bad2f86 Refactor random data generation
Add new RandomDataProvider object and implement existing random number
generation in terms of that.  This enables end users to supply their
own random data providers.
2013-12-14 15:17:35 -05:00

50 lines
1.0 KiB
C++

#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.
QUtil::srandom(QUtil::get_current_time() ^ 0xcccc);
this->seeded_random = true;
}
# ifdef HAVE_RANDOM
return ::random();
# else
return rand();
# endif
}
RandomDataProvider*
InsecureRandomDataProvider::getInstance()
{
static InsecureRandomDataProvider instance;
return &instance;
}