# EzRand Component

The EzRand component can be used to generate random numbers or bytes using a variety of algorithms.

## Syntax

```text
nsoftware.IPWorksEncrypt.EzRand
```

## Remarks

The EzRand component can be used to generate random numbers or bytes using a variety of algorithms and implementations.

To begin first set the [Algorithm](#algorithm-property-ezrand-component) property to the desired value. This property specifies the algorithm and implementation that will be used to generate the number or bytes. Possible choices include ISAAC, the Microsoft Crypto API, and platform specific random and secure random implementations.

Next set [Min](#min-property-ezrand-component) and [Max](#max-property-ezrand-component) to define the acceptable range of values when generating an integer. The [Seed](#seed-property-ezrand-component) property may optionally be set. Then simply call [GetNextInt](#getnextint-method-ezrand-component) to generate a random number. the [RandInt](#randint-property-ezrand-component) property will be populated with the generated value.

To generate a random set of bytes set [RandBytesLength](#randbyteslength-property-ezrand-component) to the desired number of bytes and call [GetNextBytes](#getnextbytes-method-ezrand-component).

## Property List

*The following is the full list of the properties of the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [Algorithm](#algorithm-property-ezrand-component) | The random number algorithm. |
| [Max](#max-property-ezrand-component) | The exclusive upper bound. |
| [Min](#min-property-ezrand-component) | The inclusive lower bound. |
| [RandBytes](#randbytes-property-ezrand-component) | The random byte array. |
| [RandBytesLength](#randbyteslength-property-ezrand-component) | The length of the byte array to be generated. |
| [RandInt](#randint-property-ezrand-component) | The random integer. |
| [Seed](#seed-property-ezrand-component) | The seed. |

## Method List

*The following is the full list of the methods of the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [Config](#config-method-ezrand-component) | Sets or retrieves a configuration setting. |
| [GetNextBytes](#getnextbytes-method-ezrand-component) | Generates a sequence of random bytes. |
| [GetNextInt](#getnextint-method-ezrand-component) | Generates a random integer. |
| [Reset](#reset-method-ezrand-component) | Resets the component. |

## Event List

*The following is the full list of the events fired by the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [Error](#error-event-ezrand-component) | Fired when information is available about errors during data delivery. |

## Config Settings

*The following is a list of config settings for the component with short descriptions. Click on the links for further details.*

|  |  |
| --- | --- |
| [OutputEncoding](#OutputEncoding) | The encoding applied to the generated bytes. |
| [BuildInfo](#BuildInfo) | Information about the product's build. |
| [GUIAvailable](#GUIAvailable) | Whether or not a message loop is available for processing events. |
| [LicenseInfo](#LicenseInfo) | Information about the current license. |
| [MaskSensitiveData](#MaskSensitiveData) | Whether sensitive data is masked in log messages. |
| [UseFIPSCompliantAPI](#UseFIPSCompliantAPI) | Tells the component whether or not to use FIPS certified APIs. |
| [UseInternalSecurityAPI](#UseInternalSecurityAPI) | Whether or not to use the system security libraries or an internal implementation. |

# Algorithm Property ([EzRand](#ezrand-component) Component)

The random number algorithm.

## Syntax

```text
public EzRandAlgorithms Algorithm { get; set; }
enum EzRandAlgorithms {
  raISAAC,
  raMSCryptoAPI,
  raPlatform,
  raSecurePlatform,
  raRC4Random
}
```

## Default Value

0

## Remarks

This property specifies the algorithm used to generate the random number or bytes. Possible values are:

|  |  |
| --- | --- |
| 0 (raISAAC) | ISAAC (indirection, shift, accumulate, add, and count) |
| 1 (raMSCryptoAPI) | The Microsoft Crypto API. This is only available on Windows. |
| 2 (raPlatform) | The platform's random implementation. |
| 3 (raSecurePlatform) | The platform's secure random implementation. This is only applicable in .NET and Java. In .NET the component uses the "RNGCryptoServiceProvider" class. In Java the component uses the "SecureRandom" class. |
| 4 (raRC4Random) | RC4 based random implementation. |

# Max Property ([EzRand](#ezrand-component) Component)

The exclusive upper bound.

## Syntax

```text
public int Max { get; set; }
```

## Default Value

100

## Remarks

This property specifies the exclusive upper bound of the random integer to be generated. The value must be greater than [Min](#min-property-ezrand-component). The default value is 100.

# Min Property ([EzRand](#ezrand-component) Component)

The inclusive lower bound.

## Syntax

```text
public int Min { get; set; }
```

## Default Value

0

## Remarks

This property specifies the inclusive lower bound of the random integer to be generated. The value must be less than [Max](#max-property-ezrand-component), and must not be negative. The default value is 0.

# RandBytes Property ([EzRand](#ezrand-component) Component)

The random byte array.

## Syntax

```text
public string RandBytes { get; }
public byte[] RandBytesB { get; }
```

## Default Value

""

## Remarks

This property holds the random byte array generated by calling [GetNextBytes](#getnextbytes-method-ezrand-component).

This property is read-only.

# RandBytesLength Property ([EzRand](#ezrand-component) Component)

The length of the byte array to be generated.

## Syntax

```text
public int RandBytesLength { get; set; }
```

## Default Value

16

## Remarks

This property specifies the length of the random byte array to be generated. The [RandBytes](#randbytes-property-ezrand-component) property will hold the byte array after [GetNextBytes](#getnextbytes-method-ezrand-component) is called. The default value is 16.

# RandInt Property ([EzRand](#ezrand-component) Component)

The random integer.

## Syntax

```text
public int RandInt { get; }
```

## Default Value

0

## Remarks

This property holds the random integer generated by calling [GetNextInt](#getnextint-method-ezrand-component).

This property is read-only.

# Seed Property ([EzRand](#ezrand-component) Component)

The seed.

## Syntax

```text
public string Seed { get; set; }
public byte[] SeedB { get; set; }
```

## Default Value

""

## Remarks

This property specifies the seed. This value is optional. If not specified and a seed is required the component will use the current time.

**Seed Notes**

- When [Algorithm](#algorithm-property-ezrand-component) is set to 0 (raISAAC) the component uses the leftmost 256 bytes.
- When [Algorithm](#algorithm-property-ezrand-component) is set to 1 (raMSCryptoAPI) the seed is ignored.
- When [Algorithm](#algorithm-property-ezrand-component) is set to 2 (raPlatform) the component uses the leftmost 4 bytes.
- When [Algorithm](#algorithm-property-ezrand-component) is set to 3 (raSecurePlatform) in .NET the seed is ignored. The seed is applicable in Java.

# Config Method ([EzRand](#ezrand-component) Component)

Sets or retrieves a configuration setting.

## Syntax

```text
public string Config(string configurationString);

Async Version
public async Task<string> Config(string configurationString);
public async Task<string> Config(string configurationString, CancellationToken cancellationToken);
```

## Remarks

Config is a generic method available in every component. It is used to set and retrieve [configuration settings](#config-settings-ezrand-component) for the component.

These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the component, access to these *internal properties* is provided through the Config method.

To set a configuration setting named *PROPERTY*, you must call *Config("PROPERTY=VALUE")*, where *VALUE* is the value of the setting expressed as a string. For boolean values, use the strings "True", "False", "0", "1", "Yes", or "No" (case does not matter).

To read (query) the value of a [configuration setting](#config-settings-ezrand-component), you must call *Config("PROPERTY")*. The value will be returned as a string.

# GetNextBytes Method ([EzRand](#ezrand-component) Component)

Generates a sequence of random bytes.

## Syntax

```text
public void GetNextBytes();

Async Version
public async Task GetNextBytes();
public async Task GetNextBytes(CancellationToken cancellationToken);
```

## Remarks

This method generates a new sequence of random bytes. The [RandBytesLength](#randbyteslength-property-ezrand-component) property specifies the length of the byte array. After calling this method the [RandBytes](#randbytes-property-ezrand-component) will hold the random bytes. The following properties are applicable when calling this method:

- [Algorithm](#algorithm-property-ezrand-component)
- [RandBytesLength](#randbyteslength-property-ezrand-component)
- [Seed](#seed-property-ezrand-component)

# GetNextInt Method ([EzRand](#ezrand-component) Component)

Generates a random integer.

## Syntax

```text
public void GetNextInt();

Async Version
public async Task GetNextInt();
public async Task GetNextInt(CancellationToken cancellationToken);
```

## Remarks

This method generates a random integer. The [Min](#min-property-ezrand-component) and [Max](#max-property-ezrand-component) properties define the minimum and maximum values. After calling this method the [RandInt](#randint-property-ezrand-component) property will hold the integer. The following properties are applicable when calling this method:

- [Algorithm](#algorithm-property-ezrand-component)
- [Min](#min-property-ezrand-component)
- [Max](#max-property-ezrand-component)
- [Seed](#seed-property-ezrand-component)

# Reset Method ([EzRand](#ezrand-component) Component)

Resets the component.

## Syntax

```text
public void Reset();

Async Version
public async Task Reset();
public async Task Reset(CancellationToken cancellationToken);
```

## Remarks

When called, the component will reset all of its properties to their default values.

# Error Event ([EzRand](#ezrand-component) Component)

Fired when information is available about errors during data delivery.

## Syntax

```text
public event OnErrorHandler OnError;

public delegate void OnErrorHandler(object sender, EzRandErrorEventArgs e);

public class EzRandErrorEventArgs : EventArgs {
  public int ErrorCode { get; }
  public string Description { get; }
}
```

## Remarks

The Error event is fired in case of exceptional conditions during message processing. Normally the component throws an exception.

The *ErrorCode* parameter contains an error code, and the *Description* parameter contains a textual description of the error. For a list of valid error codes and their descriptions, please refer to the [Error Codes](#trappable-errors-ezrand-component) section.

# Config Settings ([EzRand](#ezrand-component) Component)

 The component accepts one or more of the following *configuration settings*. Configuration settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the component, access to these *internal properties* is provided through the [Config](#config-method-ezrand-component) method.

### EzRand Config Settings

**OutputEncoding**: The encoding applied to the generated bytes.This setting optionally specifies an encoding to apply to the bytes generated when [GetNextBytes](#getnextbytes-method-ezrand-component) is called. This may be useful in situations where the bytes are displayed or transmitted as text. Possible values are:

- 0 (none - default)
- 1 (Base64)
- 2 (Hex)
- 3 (Base64URL)

The [RandBytes](#randbytes-property-ezrand-component) property will hold the string.

### Base Config Settings

**BuildInfo**: Information about the product's build.When queried, this setting will return a string containing information about the product's build.

**GUIAvailable**: Whether or not a message loop is available for processing events.In a GUI-based application, long-running blocking operations may cause the application to stop responding to input until the operation returns. The component will attempt to discover whether or not the application has a message loop and, if one is discovered, it will process events in that message loop during any such blocking operation.

In some non-GUI applications, an invalid message loop may be discovered that will result in errant behavior. In these cases, setting [GUIAvailable](#GUIAvailable) to *false* will ensure that the component does not attempt to process external events.

**LicenseInfo**: Information about the current license.When queried, this setting will return a string containing information about the license this instance of a component is using. It will return the following information:

- Product: The product the license is for.
- Product Key: The key the license was generated from.
- License Source: Where the license was found (e.g., RuntimeLicense, License File).
- License Type: The type of license installed (e.g., Royalty Free, Single Server).
- Last Valid Build: The last valid build number for which the license will work.

**MaskSensitiveData**: Whether sensitive data is masked in log messages.In certain circumstances it may be beneficial to mask sensitive data, like passwords, in log messages. Set this to *true* to mask sensitive data. The default is *true*.

**UseFIPSCompliantAPI**: Tells the component whether or not to use FIPS certified APIs.When set to *true*, the component will utilize the underlying operating system's certified APIs. Java editions, regardless of OS, utilize Bouncy Castle Federal Information Processing Standards (FIPS), while all other Windows editions make use of Microsoft security libraries.

FIPS mode can be enabled by setting the *UseFIPSCompliantAPI* configuration setting to *true*. This is a static setting that applies to all instances of all components of the toolkit within the process. It is recommended to enable or disable this setting once before the component has been used to establish a connection. Enabling FIPS while an instance of the component is active and connected may result in unexpected behavior.

For more details, please see the [FIPS 140-2 Compliance](https://www.nsoftware.com/kb/articles/fips.rst) article.

NOTE: This setting is applicable only on Windows.

NOTE: Enabling FIPS compliance requires a special license; please contact [sales@nsoftware.com](mailto:sales@nsoftware.com) for details.

**UseInternalSecurityAPI**: Whether or not to use the system security libraries or an internal implementation. When set to *false*, the component will use the system security libraries by default to perform cryptographic functions where applicable. In this case, calls to unmanaged code will be made. In certain environments, this is not desirable. To use a completely managed security implementation, set this setting to *true*.

Setting this configuration setting to *true* tells the component to use the internal implementation instead of using the system security libraries.

 On Windows, this setting is set to *false* by default. On Linux/macOS, this setting is set to *true* by default.

NOTE: This setting is static. The value set is applicable to all components used in the application.

When this value is set, the product's system dynamic link library (DLL) is no longer required as a reference, as all unmanaged code is stored in that file.

# Trappable Errors ([EzRand](#ezrand-component) Component)

### EzRand Errors

|  |  |
| --- | --- |
| 101 | Unsupported algorithm. |
| 105 | Invalid Min, Max, or RandomBytesLength value. |
