2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-16 01:27:07 +00:00
qpdf/libqpdf/InsecureRandomDataProvider.cc
Jay Berkenbilt cb769c62e5 WHITESPACE ONLY -- expand tabs in source code
This comment expands all tabs using an 8-character tab-width. You
should ignore this commit when using git blame or use git blame -w.

In the early days, I used to use tabs where possible for indentation,
since emacs did this automatically. In recent years, I have switched
to only using spaces, which means qpdf source code has been a mixture
of spaces and tabs. I have avoided cleaning this up because of not
wanting gratuitous whitespaces change to cloud the output of git
blame, but I changed my mind after discussing with users who view qpdf
source code in editors/IDEs that have other tab widths by default and
in light of the fact that I am planning to start applying automatic
code formatting soon.
2022-02-08 11:51:15 -05:00

56 lines
1.2 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.
unsigned int 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;
}