TSDuck v3.40-4033
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::BetterSystemRandomGenerator Class Reference

Improved system-provided PRNG (pseudo-random numbers generator). More...

#include <tsBetterSystemRandomGenerator.h>

Inheritance diagram for ts::BetterSystemRandomGenerator:
Collaboration diagram for ts::BetterSystemRandomGenerator:

Public Member Functions

virtual UString name () const override
 Algorithm name (informational only).
 
virtual bool read (void *, size_t) override
 Get random data.
 
virtual bool readByteBlock (ByteBlock &data, size_t size)
 Get random data in a byte block.
 
template<typename INT_T >
requires std::integral<INT_T>
bool readInt (INT_T &value, INT_T min=std::numeric_limits< INT_T >::min(), INT_T max=std::numeric_limits< INT_T >::max())
 Get a random integer value inside a defined range.
 
virtual bool ready () const override
 Check if the PRNG is ready.
 
virtual bool seed (const void *, size_t) override
 Seed (add entropy) to the PRNG.
 

Static Public Member Functions

static BetterSystemRandomGeneratorInstance ()
 Get the instance of the singleton of this class.
 

Detailed Description

Improved system-provided PRNG (pseudo-random numbers generator).

Use SystemRandomGenerator as base and add AES-based post-processing. Implemented as a thread-safe singleton.

An entropy state is stored in $HOME/.tsseed. The value of a state is initially loaded from this file. The first time the generator is used (no file), the state is loaded from the system PRNG.

A fixed AES-128 key K is used for the post-processing. The size of the state is 16 bytes, the AES block size.

Description of post-processing, added to the sytem PRNG:

Known limitations:

  • The entropy file is rewritten after each block => poor performances.
  • Concurrent processes overwrite the same .tsseed file.

Member Function Documentation

◆ Instance()

static BetterSystemRandomGenerator & ts::BetterSystemRandomGenerator::Instance ( )
inlinestatic

Get the instance of the singleton of this class.

Returns
The instance of the singleton of this class.

◆ name()

virtual UString ts::BetterSystemRandomGenerator::name ( ) const
overridevirtual

Algorithm name (informational only).

Returns
The algorithm name.

Reimplemented from ts::SystemRandomGenerator.

◆ ready()

virtual bool ts::BetterSystemRandomGenerator::ready ( ) const
overridevirtual

Check if the PRNG is ready.

If not ready, it must be seeded again.

Returns
True if the PRNG is ready.

Reimplemented from ts::SystemRandomGenerator.

◆ read()

virtual bool ts::BetterSystemRandomGenerator::read ( void *  addr,
size_t  size 
)
overridevirtual

Get random data.

Parameters
[out]addrAddress of returned random data.
[in]sizeRequested size in bytes of random data. The method always fill the buffer.
Returns
True on success, false on error.

Reimplemented from ts::SystemRandomGenerator.

◆ seed()

virtual bool ts::SystemRandomGenerator::seed ( const void *  addr,
size_t  size 
)
overridevirtualinherited

Seed (add entropy) to the PRNG.

Parameters
[in]addrAddress of entropy data.
[in]sizeSize in bytes of entropy data.
Returns
True on success, false on error.

Implements ts::RandomGenerator.

◆ readByteBlock()

virtual bool ts::RandomGenerator::readByteBlock ( ByteBlock data,
size_t  size 
)
virtualinherited

Get random data in a byte block.

The default implementation uses read().

Parameters
[out]dataReturned random data.
[in]sizeRequested size in bytes of random data.
Returns
True on success, false on error.

◆ readInt()

template<typename INT_T >
requires std::integral<INT_T>
bool ts::RandomGenerator::readInt ( INT_T &  value,
INT_T  min = std::numeric_limits<INT_T>::min(),
INT_T  max = std::numeric_limits<INT_T>::max() 
)
inherited

Get a random integer value inside a defined range.

Template Parameters
INT_TAn integer type for the result.
Parameters
[out]valueThe return integer value.
[in]minMinimum value to return.
[in]maxMaximum value to return.
Returns
True on success, false on error.

The documentation for this class was generated from the following file: