C12Adapter Opensource C++ Interface
MRandomGenerator Class Reference

Cryptographically secure random generator. More...

Inheritance diagram for MRandomGenerator:

Public Member Functions

 MRandomGenerator ()
 Create the object so it can be used further for random generation. More...
 
virtual ~MRandomGenerator ()
 Destroy generator object.
 
MByteString Generate (unsigned size)
 Generate a row of random bytes of a requested length. More...
 
void GenerateBuffer (char *buff, unsigned size)
 Generate random bytes into a buffer provided. More...
 
- Public Member Functions inherited from MObject
virtual ~MObject ()
 Object destructor.
 
virtual const MClassGetClass () const =0
 Get the final class of the object. More...
 
virtual unsigned GetEmbeddedSizeof () const
 For embedded object types, return the size of the class. More...
 
bool IsEmbeddedObject () const
 Tell if the object is of embedded kind. More...
 
SHOW_INTERNAL MVariant Call (const MStdString &name, const MVariant &params)
 Call the object service with parameters, given as variant. More...
 
MVariant Call0 (const MStdString &name)
 Call the object service with no parameters. More...
 
MVariant Call1 (const MStdString &name, const MVariant &p1)
 Call the object service with one parameter. More...
 
MVariant Call2 (const MStdString &name, const MVariant &p1, const MVariant &p2)
 Call the object service with two parameter. More...
 
MVariant Call3 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3)
 Call the object service with three parameter. More...
 
MVariant Call4 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4)
 Call the object service with four parameter. More...
 
MVariant Call5 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5)
 Call the object service with five parameter. More...
 
MVariant Call6 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5, const MVariant &p6)
 Call the object service with six parameter. More...
 
virtual MVariant CallV (const MStdString &name, const MVariant::VariantVector &params)
 Call the object service with parameters, given as variant vector. More...
 
virtual bool IsPropertyPresent (const MStdString &name) const
 Tell if the property with the given name exists.
 
virtual bool IsServicePresent (const MStdString &name) const
 Tell if the service with the given name exists.
 
virtual MVariant GetProperty (const MStdString &name) const
 Get the property value using name of the property. More...
 
virtual void SetProperty (const MStdString &name, const MVariant &value)
 Set the property using name of the property, and value. More...
 
virtual MStdStringVector GetAllPropertyNames () const
 Return the list of publicly available properties, persistent or not. More...
 
virtual MStdStringVector GetAllPersistentPropertyNames () const
 Return the list of persistent properties. More...
 
virtual void SetPersistentPropertiesToDefault ()
 Set the persistent properties of the object to their default values. More...
 
virtual MVariant GetPersistentPropertyDefaultValue (const MStdString &name) const
 Get the default value of persistent property with the name given. More...
 
virtual void SetPersistentPropertyToDefault (const MStdString &name)
 Set the persistent property with the name given to default value. More...
 
virtual const char * GetType () const
 Get the name of the type for the object (could be the same as class name).
 
virtual void SetType (const MStdString &)
 Intentionally, it will set the name of the type for the object, but the service will not allow setting the name to anything other than the current name. More...
 
virtual void Validate ()
 Validate internal structures of the object. More...
 

Static Public Member Functions

static MByteString StaticGenerate (unsigned size)
 Convenience method that opens the generator, generates a byte string, and closes the generator, all in a single call. More...
 
static void StaticGenerateBuffer (char *buff, unsigned size)
 Convenience method that opens the generator, generates a sequence into a given buffer, and closes the generator, all in a single call. More...
 
- Static Public Member Functions inherited from MObject
static const MClassGetStaticClass ()
 Get the declared class of this particular object. More...
 
static bool IsClassPresent (const MStdString &name)
 Tells if the given class name is available. More...
 

Additional Inherited Members

- Static Public Attributes inherited from MObject
static const MClass s_class
 Class of MObject.
 
- Protected Member Functions inherited from MObject
 MObject ()
 Object constructor, protected as the class is abstract.
 
void DoSetPersistentPropertiesToDefault (const MClass *staticClass)
 Set the persistent properties to their default values for one object provided the class for that object. More...
 

Detailed Description

Cryptographically secure random generator.

The implementation uses operating system services to offer raw buffers filled with random bytes that can be used in cryptography.

See also
MMath::Rand() - not cryptographically secure but much faster random number generator function that returns integer
MMath::RandomInRange() - not cryptographically secure but much faster random number generator function that returns unsigned integer within a desired range

Constructor & Destructor Documentation

MRandomGenerator::MRandomGenerator ( )
inline

Create the object so it can be used further for random generation.

This constructor delays initialization of the generator to the first use of Generate function. It will never throw any sort of error.

Member Function Documentation

MByteString MRandomGenerator::Generate ( unsigned  size)

Generate a row of random bytes of a requested length.

When called the first time after construction of the object, the method is going to initialize the operating system random generator service, therefore, the first call can take longer, and it can generate an extended set of errors.

Parameters
sizeNumber of random bytes to generate and return Size shall not be zero, and shall be less than INT_MAX, which is approximately two billion, or an exception is thrown.
Returns
MByteString - the string of bytes with random values
See also
StaticGenerate - convenience method that opens the generator, generates a sequence, and closes the generator, all in a single call
void MRandomGenerator::GenerateBuffer ( char *  buff,
unsigned  size 
)

Generate random bytes into a buffer provided.

When called first time after construction of the object, the method is going to initialize the operating system random generator service, therefore, the first call can take longer, and it can generate an extended set of errors.

Parameters
buffBuffer into which the numbers shall be generated, shall be no less than the size given, or the behavior is undefined.
sizeNumber of random bytes to generate and return. Size shall not be zero, and shall be less than INT_MAX, which is approximately two billion, or an exception is thrown.
Returns
MByteString - the string of bytes with random values
See also
Generate - generate a random sequence into MByteString
StaticGenerateBuffer - same as StaticGenerate, but do the job into a specified buffer
static MByteString MRandomGenerator::StaticGenerate ( unsigned  size)
static

Convenience method that opens the generator, generates a byte string, and closes the generator, all in a single call.

It makes sense to use this method if only one single random raw buffer of bytes has to be generated, otherwise, the method will be doing generator initialization at every call, which is extra time and resource.

Parameters
sizeNumber of random bytes to generate and return. Size shall not be zero, and shall be less than INT_MAX, which is approximately two billion, or an exception is thrown.
Returns
MByteString - the string of bytes with random values
See also
Generate - non-static method that will be faster if multiple sequences have to be generated
Since
MeteringSDK Version 5.0.0.1230.
static void MRandomGenerator::StaticGenerateBuffer ( char *  buff,
unsigned  size 
)
static

Convenience method that opens the generator, generates a sequence into a given buffer, and closes the generator, all in a single call.

It makes sense to use this method if only one single random raw buffer of bytes has to be generated, otherwise, the method will be doing generator initialization at every call, which is extra time and resource.

Parameters
buffBuffer into which the numbers shall be generated, shall be no less than the size given, or the behavior is undefined.
sizeNumber of random bytes to generate and return. Size shall not be zero, and shall be less than INT_MAX, which is approximately two billion, or an exception is thrown.
See also
Generate - non-static method that will be faster if multiple sequences have to be generated
StaticGenerate - same as this method, but it returns a MByteString
Since
MeteringSDK Version 5.0.0.1230.