poser
A C framework for POsix SERvices
Loading...
Searching...
No Matches
PSC_Random Class Reference

Get some random data. More...

#include <poser/core/random.h>

Static Public Member Functions

size_t PSC_Random_bytes (uint8_t *buf, size_t count, PSC_RandomFlags flags)
 Fill a buffer with random bytes.
 
size_t PSC_Random_string (char *str, size_t size, PSC_RandomFlags flags)
 Fill a buffer with a random string.
 
char * PSC_Random_createStr (size_t count, PSC_RandomFlags flags)
 Create a newly allocated random string.
 

Detailed Description

Get some random data.

Provides static methods to obtain random bytes and random strings. If available, arc4random() is used for that, which is expected to never block, never fail and return cryptographically secure random data.

If arc4random is not available, several other methods are tried.

  • If available, getrandom() is the first fallback, with flags matching the flags specified in the call to PSC_Random.
  • If this is unavailable or fails, reading from /dev/urandom or /dev/random is tried if applicable given the flags specified.
  • Finally, if cryptographically secure randomness is not specifically requested, an internal xorshift-based PRNG is used.

Member Function Documentation

◆ PSC_Random_bytes()

size_t PSC_Random_bytes ( uint8_t *  buf,
size_t  count,
PSC_RandomFlags  flags 
)
static

Fill a buffer with random bytes.

This never fails unless PSC_RF_SECURE is given and arc4random() is not available.

Parameters
bufthe buffer to fill
countthe number of random bytes put into buf
flagsflags controlling methods used
Returns
the number of bytes written, which will be less than count on error

◆ PSC_Random_createStr()

char * PSC_Random_createStr ( size_t  count,
PSC_RandomFlags  flags 
)
static

Create a newly allocated random string.

The string is constructed by base64-encoding random bytes. Memory for the new string is allocated, so the caller has to free() it later. This never fails unless PSC_RF_SECURE is given and arc4random() is not available.

Parameters
countuse this many random bytes to construct the string
flagsflags controlling methods used
Returns
the new random string, or NULL on error

◆ PSC_Random_string()

size_t PSC_Random_string ( char *  str,
size_t  size,
PSC_RandomFlags  flags 
)
static

Fill a buffer with a random string.

The string is constructed by base64-encoding random bytes. This never fails unless PSC_RF_SECURE is given and arc4random() is not available.

Parameters
strthe buffer to put the string into
sizethe length of the string, including a terminating NUL
flagsflags controlling methods used
Returns
the string size (including a terminating NUL) actually written, which will be less than size on error

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