Prng Delphi DLL Reference Documentation

Prng

Current Version: 10.0.0

Provides secure pseudo-random number generation (PRNG). Implements the Fortuna PRNG algorithm using 256-bit AES and SHA256. Also provides methods for accessing sources of system entropy.

Create/Dispose

var
myObject: HCkPrng;

begin
myObject := CkPrng_Create();

// ...

CkPrng_Dispose(myObject);
end;
function CkPrng_Create: HCkPrng; stdcall;

Creates an instance of the HCkPrng object and returns a handle (i.e. a Pointer). The handle is passed in the 1st argument for the functions listed on this page.

procedure CkPrng_Dispose(handle: HCkPrng); stdcall;

Objects created by calling CkPrng_Create must be freed by calling this method. A memory leak occurs if a handle is not disposed by calling this function.

Properties

DebugLogFilePath
procedure CkPrng_getDebugLogFilePath(objHandle: HCkPrng; outPropVal: HCkString); stdcall;
procedure CkPrng_putDebugLogFilePath(objHandle: HCkPrng; newPropVal: PWideChar); stdcall;
function CkPrng__debugLogFilePath(objHandle: HCkPrng): PWideChar; stdcall;

If set to a file path, causes each Chilkat method or property call to automatically append it's LastErrorText to the specified log file. The information is appended such that if a hang or crash occurs, it is possible to see the context in which the problem occurred, as well as a history of all Chilkat calls up to the point of the problem. The VerboseLogging property can be set to provide more detailed information.

This property is typically used for debugging the rare cases where a Chilkat method call hangs or generates an exception that halts program execution (i.e. crashes). A hang or crash should generally never happen. The typical causes of a hang are:

  1. a timeout related property was set to 0 to explicitly indicate that an infinite timeout is desired,
  2. the hang is actually a hang within an event callback (i.e. it is a hang within the application code), or
  3. there is an internal problem (bug) in the Chilkat code that causes the hang.

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top
LastErrorHtml
procedure CkPrng_getLastErrorHtml(objHandle: HCkPrng; outPropVal: HCkString); stdcall;
function CkPrng__lastErrorHtml(objHandle: HCkPrng): PWideChar; stdcall;

Provides information in HTML format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

See the notes about PWideChar memory ownership and validity.

top
LastErrorText
procedure CkPrng_getLastErrorText(objHandle: HCkPrng; outPropVal: HCkString); stdcall;
function CkPrng__lastErrorText(objHandle: HCkPrng): PWideChar; stdcall;

Provides information in plain-text format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

See the notes about PWideChar memory ownership and validity.

top
LastErrorXml
procedure CkPrng_getLastErrorXml(objHandle: HCkPrng; outPropVal: HCkString); stdcall;
function CkPrng__lastErrorXml(objHandle: HCkPrng): PWideChar; stdcall;

Provides information in XML format about the last method/property called. If a method call returns a value indicating failure, or behaves unexpectedly, examine this property to get more information.

See the notes about PWideChar memory ownership and validity.

top
LastMethodSuccess
function CkPrng_getLastMethodSuccess(objHandle: HCkPrng): wordbool; stdcall;
procedure CkPrng_putLastMethodSuccess(objHandle: HCkPrng; newPropVal: wordbool); stdcall;

Indicate whether the last method call succeeded or failed. A value of True indicates success, a value of False indicates failure. This property is automatically set for method calls. It is not modified by property accesses. The property is automatically set to indicate success for the following types of method calls:

  • Any method that returns a string.
  • Any method returning a Chilkat object, binary bytes, or a date/time.
  • Any method returning a standard boolean status value where success = True and failure = False.
  • Any method returning an integer where failure is defined by a return value less than zero.

Note: Methods that do not fit the above requirements will always set this property equal to True. For example, a method that returns no value (such as a "void" in C++) will technically always succeed.

top
PrngName
procedure CkPrng_getPrngName(objHandle: HCkPrng; outPropVal: HCkString); stdcall;
procedure CkPrng_putPrngName(objHandle: HCkPrng; newPropVal: PWideChar); stdcall;
function CkPrng__prngName(objHandle: HCkPrng): PWideChar; stdcall;
Introduced in version 9.5.0.52

The name of the PRNG selected. Currently, the default and only possible value is "fortuna". See the links below for information about the Fortuna PRNG.

Note: Because "fortuna" is the only valid choice, assigning this property to a different value will always be ignored (until alternative PRNG algorithms are added in the future).

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top
VerboseLogging
function CkPrng_getVerboseLogging(objHandle: HCkPrng): wordbool; stdcall;
procedure CkPrng_putVerboseLogging(objHandle: HCkPrng; newPropVal: wordbool); stdcall;

If set to True, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is False. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
procedure CkPrng_getVersion(objHandle: HCkPrng; outPropVal: HCkString); stdcall;
function CkPrng__version(objHandle: HCkPrng): PWideChar; stdcall;

Version of the component/library, such as "9.5.0.94"

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top

Methods

AddEntropy
function CkPrng_AddEntropy(objHandle: HCkPrng;
    entropy: PWideChar;
    encoding: PWideChar): wordbool; stdcall;
Introduced in version 9.5.0.52

Adds entropy to the PRNG (i.e. adds more seed material to the PRNG). Entropy can be obtained by calling GetEntropy, or the application might have its own sources for obtaining entropy. An application may continue to add entropy at desired intervals. How the entropy is used depends on the PRNG algorithm. For Fortuna, the entropy is added to the internal entropy pools and used when internal automatic reseeding occurs.

An application may add non-random entropy for testing purposes. This allows for the reproduction of the same pseudo-random number sequence for testing and debugging purposes.

The entropy bytes are passed in entropy using the binary encoding specified in encoding. Binary encodings can be "hex", "base64", etc. See the link below for supported binary encodings.

Returns True for success, False for failure.

top
AddEntropyBytes
function CkPrng_AddEntropyBytes(objHandle: HCkPrng;
    entropy: HCkByteData): wordbool; stdcall;
Introduced in version 9.5.0.52

Adds entropy to the PRNG (i.e. adds more seed material to the PRNG). Entropy can be obtained by calling GetEntropy, or the application might have its own sources for obtaining entropy. An application may continue to add entropy at desired intervals. How the entropy is used depends on the PRNG algorithm. For Fortuna, the entropy is added to the internal entropy pools and used when internal automatic reseeding occurs.

An application may add non-random entropy for testing purposes. This allows for the reproduction of the same pseudo-random number sequence for testing and debugging purposes.

Returns True for success, False for failure.

top
ExportEntropy
function CkPrng_ExportEntropy(objHandle: HCkPrng;
    outStr: HCkString): wordbool; stdcall;
function CkPrng__exportEntropy(objHandle: HCkPrng): PWideChar; stdcall;
Introduced in version 9.5.0.52

Exports all accumulated entropy and returns it in a base64 encoded string. (Internally the entropy pools are re-hashed so that a hacker cannot determine the state of the PRNG even if the exported entropy was obtained.) When a system restarts it can import what was previously exported by calling ImportEntropy. This ensures an adequate amount of entropy is immediately available when first generating random bytes.

For example, an application could persist the exported entropy to a database or file. When the application starts again, it could import the persisted entropy, add some entropy from a system source (via the GetEntropy/AddEntropy methods), and then begin generating random data.

Returns True for success, False for failure.

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top
FirebasePushId
function CkPrng_FirebasePushId(objHandle: HCkPrng;
    outStr: HCkString): wordbool; stdcall;
function CkPrng__firebasePushId(objHandle: HCkPrng): PWideChar; stdcall;
Introduced in version 9.5.0.58

Generates a random Firebase Push ID. See Firebase Unique Identifiers.

Returns True for success, False for failure.

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top
GenRandom
function CkPrng_GenRandom(objHandle: HCkPrng;
    numBytes: Integer;
    encoding: PWideChar;
    outStr: HCkString): wordbool; stdcall;
function CkPrng__genRandom(objHandle: HCkPrng;
    numBytes: Integer;
    encoding: PWideChar): PWideChar; stdcall;
Introduced in version 9.5.0.52

Generates and returns numBytes random bytes in encoded string form. The binary encoding is specified by encoding, and can be "hex", "base64", etc. (See the link below for supported binary encodings.)

Important: If no entropy was explicitly added prior to first call to generate random bytes, then 32 bytes of entropy (from the system source, such as /dev/random) are automatically added to seed the PRNG.

Returns True for success, False for failure.

See the notes about PWideChar memory ownership and validity.

top
GenRandomBd
function CkPrng_GenRandomBd(objHandle: HCkPrng;
    numBytes: Integer;
    bd: HCkBinData): wordbool; stdcall;
Introduced in version 9.5.0.66

Appends numBytes random bytes to bd.

Important: If no entropy was explicitly added prior to first call to generate random bytes, then 32 bytes of entropy (from the system source, such as /dev/random) are automatically added to seed the PRNG.

Returns True for success, False for failure.

More Information and Examples
top
GenRandomBytes
function CkPrng_GenRandomBytes(objHandle: HCkPrng;
    numBytes: Integer;
    outBytes: HCkByteData): wordbool; stdcall;
Introduced in version 9.5.0.52

Generates and returns numBytes random bytes.

Important: If no entropy was explicitly added prior to first call to generate random bytes, then 32 bytes of entropy (from the system source, such as /dev/random) are automatically added to seed the PRNG.

Returns True for success, False for failure.

top
GetEntropy
function CkPrng_GetEntropy(objHandle: HCkPrng;
    numBytes: Integer;
    encoding: PWideChar;
    outStr: HCkString): wordbool; stdcall;
function CkPrng__getEntropy(objHandle: HCkPrng;
    numBytes: Integer;
    encoding: PWideChar): PWideChar; stdcall;
Introduced in version 9.5.0.52

Reads real entropy bytes from a system entropy source and returns as an encoded string. On Linux/Unix based systems, including MAC OS X, this is accomplished by reading /dev/random. On Windows systems, it uses the Microsoft Cryptographic Service Provider's CryptGenRandom method.

It is recommended that no more than 32 bytes of entropy should be retrieved to initially seed a PRNG. Larger amounts of entropy are fairly useless. However, an app is free to periodically add bits of entropy to a long-running PRNG as it sees fit.

The encoding specifies the encoding to be used. It can be "hex", "base64", or many other possibilities. See the link below.

Returns True for success, False for failure.

See the notes about PWideChar memory ownership and validity.

top
GetEntropyBytes
function CkPrng_GetEntropyBytes(objHandle: HCkPrng;
    numBytes: Integer;
    outBytes: HCkByteData): wordbool; stdcall;
Introduced in version 9.5.0.52

Reads and returns real entropy bytes from a system entropy source. On Linux/Unix based systems, including MAC OS X, this is accomplished by reading /dev/random. On Windows systems, it uses the Microsoft Cryptographic Service Provider's CryptGenRandom method.

It is recommended that no more than 32 bytes of entropy should be retrieved to initially seed a PRNG. Larger amounts of entropy are fairly useless. However, an app is free to periodically add bits of entropy to a long-running PRNG as it sees fit.

Returns True for success, False for failure.

top
ImportEntropy
function CkPrng_ImportEntropy(objHandle: HCkPrng;
    entropy: PWideChar): wordbool; stdcall;
Introduced in version 9.5.0.52

Imports entropy from previously exported entropy. See the ExportEntropy method for more information.

Returns True for success, False for failure.

More Information and Examples
top
RandomInt
function CkPrng_RandomInt(objHandle: HCkPrng;
    low: Integer;
    high: Integer): Integer; stdcall;
Introduced in version 9.5.0.52

Generates and returns a random integer between low and high (inclusive). For example, if low is 4 and high is 8, then random integers in the range 4, 5, 6, 7, 8 are returned.

More Information and Examples
top
RandomPassword
function CkPrng_RandomPassword(objHandle: HCkPrng;
    length: Integer;
    mustIncludeDigit: wordbool;
    upperAndLowercase: wordbool;
    mustHaveOneOf: PWideChar;
    excludeChars: PWideChar;
    outStr: HCkString): wordbool; stdcall;
function CkPrng__randomPassword(objHandle: HCkPrng;
    length: Integer;
    mustIncludeDigit: wordbool;
    upperAndLowercase: wordbool;
    mustHaveOneOf: PWideChar;
    excludeChars: PWideChar): PWideChar; stdcall;
Introduced in version 9.5.0.52

Generates and returns a random password of a specified length. If mustIncludeDigit is True, the generated password will contain at least one digit (0-9). If upperAndLowercase is True, then generated password will contain both lowercase and uppercase USASCII chars (a-z and A-Z). If mustHaveOneOf is a non-empty string, it contains the set of non-alphanumeric characters, one of which must be included in the password. For example, mustHaveOneOf might be the string "!@#$%". If excludeChars is a non-empty string, it contains chars that should be excluded from the password. A typical need would be to exclude chars that appear similar to others, such as i, l, 1, L, o, 0, O.

Returns True for success, False for failure.

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top
RandomString
function CkPrng_RandomString(objHandle: HCkPrng;
    length: Integer;
    bDigits: wordbool;
    bLower: wordbool;
    bUpper: wordbool;
    outStr: HCkString): wordbool; stdcall;
function CkPrng__randomString(objHandle: HCkPrng;
    length: Integer;
    bDigits: wordbool;
    bLower: wordbool;
    bUpper: wordbool): PWideChar; stdcall;
Introduced in version 9.5.0.52

Generates and returns a random string that may contain digits (0-9), lowercase ASCII (a-z) , and uppercase ASCII (A-Z). To include numeric digits, set bDigits equal to True. To include lowercase ASCII, set bLower equal to True. To include uppercase ASCII, set bUpper equal to True. The length of the string to be generated is specified by length.

Returns True for success, False for failure.

See the notes about PWideChar memory ownership and validity.

More Information and Examples
top