EzRand Class
Properties Methods Events Config Settings Errors
The EzRand class can be used to generate random numbers or bytes using a variety of algorithms.
Syntax
ipworksencrypt.EzRand
Remarks
The EzRand class can be used to generate random numbers or bytes using a variety of algorithms and implementations.
To begin first set the Algorithm 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 and Max to define the acceptable range of values when generating an integer. The Seed property may optionally be set. Then simply call GetNextInt to generate a random number. the RandInt property will be populated with the generated value.
To generate a random set of bytes set RandBytesLength to the desired number of bytes and call GetNextBytes.
Property List
The following is the full list of the properties of the class with short descriptions. Click on the links for further details.
Algorithm | The random number algorithm. |
Max | The exclusive upper bound. |
Min | The inclusive lower bound. |
RandBytes | The random byte array. |
RandBytesLength | The length of the byte array to be generated. |
RandInt | The random integer. |
Seed | The seed. |
Method List
The following is the full list of the methods of the class with short descriptions. Click on the links for further details.
Config | Sets or retrieves a configuration setting. |
GetNextBytes | Generates a sequence of random bytes. |
GetNextInt | Generates a random integer. |
Reset | Resets the class. |
Event List
The following is the full list of the events fired by the class with short descriptions. Click on the links for further details.
Error | Fired when information is available about errors during data delivery. |
Config Settings
The following is a list of config settings for the class with short descriptions. Click on the links for further details.
OutputEncoding | The encoding applied to the generated bytes. |
BuildInfo | Information about the product's build. |
GUIAvailable | Whether or not a message loop is available for processing events. |
LicenseInfo | Information about the current license. |
MaskSensitiveData | Whether sensitive data is masked in log messages. |
UseDaemonThreads | Whether threads created by the class are daemon threads. |
UseFIPSCompliantAPI | Tells the class whether or not to use FIPS certified APIs. |
UseInternalSecurityAPI | Whether or not to use the system security libraries or an internal implementation. |
Algorithm Property (EzRand Class)
The random number algorithm.
Syntax
public int getAlgorithm(); public void setAlgorithm(int algorithm); Enumerated values: public final static int raISAAC = 0; public final static int raMSCryptoAPI = 1; public final static int raPlatform = 2; public final static int raSecurePlatform = 3; public final static int raRC4Random = 4;
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 class uses the "RNGCryptoServiceProvider" class. In Java the class uses the "SecureRandom" class. |
4 (raRC4Random) | RC4 based random implementation. |
Max Property (EzRand Class)
The exclusive upper bound.
Syntax
public int getMax(); public void setMax(int max);
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. The default value is 100.
Min Property (EzRand Class)
The inclusive lower bound.
Syntax
public int getMin(); public void setMin(int min);
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, and must not be negative. The default value is 0.
RandBytes Property (EzRand Class)
The random byte array.
Syntax
public byte[] getRandBytes();
Default Value
""
Remarks
This property holds the random byte array generated by calling GetNextBytes.
This property is read-only.
RandBytesLength Property (EzRand Class)
The length of the byte array to be generated.
Syntax
public int getRandBytesLength(); public void setRandBytesLength(int randBytesLength);
Default Value
16
Remarks
This property specifies the length of the random byte array to be generated. The RandBytes property will hold the byte array after GetNextBytes is called. The default value is 16.
RandInt Property (EzRand Class)
The random integer.
Syntax
public int getRandInt();
Default Value
0
Remarks
This property holds the random integer generated by calling GetNextInt.
This property is read-only.
Seed Property (EzRand Class)
The seed.
Syntax
public byte[] getSeed(); public void setSeed(byte[] seed);
Default Value
""
Remarks
This property specifies the seed. This value is optional. If not specified and a seed is required the class will use the current time.
Seed Notes
- When Algorithm is set to 0 (raISAAC) the class uses the leftmost 256 bytes.
- When Algorithm is set to 1 (raMSCryptoAPI) the seed is ignored.
- When Algorithm is set to 2 (raPlatform) the class uses the leftmost 4 bytes.
- When Algorithm is set to 3 (raSecurePlatform) in .NET the seed is ignored. The seed is applicable in Java.
Config Method (EzRand Class)
Sets or retrieves a configuration setting.
Syntax
public String config(String configurationString);
Remarks
Config is a generic method available in every class. It is used to set and retrieve configuration settings for the class.
These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, 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, you must call Config("PROPERTY"). The value will be returned as a string.
GetNextBytes Method (EzRand Class)
Generates a sequence of random bytes.
Syntax
public void getNextBytes();
Remarks
This method generates a new sequence of random bytes. The RandBytesLength property specifies the length of the byte array. After calling this method the RandBytes will hold the random bytes. The following properties are applicable when calling this method:
GetNextInt Method (EzRand Class)
Generates a random integer.
Syntax
public void getNextInt();
Remarks
This method generates a random integer. The Min and Max properties define the minimum and maximum values. After calling this method the RandInt property will hold the integer. The following properties are applicable when calling this method:
Reset Method (EzRand Class)
Resets the class.
Syntax
public void reset();
Remarks
When called, the class will reset all of its properties to their default values.
Error Event (EzRand Class)
Fired when information is available about errors during data delivery.
Syntax
public class DefaultEzRandEventListener implements EzRandEventListener { ... public void error(EzRandErrorEvent e) {} ... } public class EzRandErrorEvent { public int errorCode; public String description; }
Remarks
The Error event is fired in case of exceptional conditions during message processing. Normally the class 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 section.
Config Settings (EzRand Class)
The class 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 class, access to these internal properties is provided through the Config method.EzRand Config Settings
- 0 (none - default)
- 1 (Base64)
- 2 (Hex)
- 3 (Base64URL)
The RandBytes property will hold the string.
Base Config Settings
In some non-GUI applications, an invalid message loop may be discovered that will result in errant behavior. In these cases, setting GUIAvailable to false will ensure that the class does not attempt to process external events.
- 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.
This setting only works on these classes: AS3Receiver, AS3Sender, Atom, Client(3DS), FTP, FTPServer, IMAP, OFTPClient, SSHClient, SCP, Server(3DS), Sexec, SFTP, SFTPServer, SSHServer, TCPClient, TCPServer.
The Java edition requires installation of the FIPS-certified Bouncy Castle library regardless of the target operating system. This can be downloaded from https://www.bouncycastle.org/fips-java/. Only the "Provider" library is needed. The jar file should then be installed in a JRE search path.
The following classes must be imported in the application in which the component will be used:
import java.security.Security;
import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider;
The Bouncy Castle provider must be added as a valid provider and must also be configured to operate in FIPS mode:
System.setProperty("org.bouncycastle.fips.approved_only","true");
Security.addProvider(new BouncyCastleFipsProvider());
When UseFIPSCompliantAPI is true, Secure Sockets Layer (SSL)-enabled classes can optionally be configured to use the Transport Layer Security (TLS) Bouncy Castle library. When SSLProvider is set to sslpAutomatic (default) or sslpInternal, an internal TLS implementation is used, but all cryptographic operations are offloaded to the Bouncy Castle FIPS provider to achieve FIPS-compliant operation. If SSLProvider is set to sslpPlatform, the Bouncy Castle JSSE will be used in place of the internal TLS implementation.
To enable the use of the Bouncy Castle JSSE take the following steps in addition to the steps above. Both the Bouncy Castle FIPS provider and the Bouncy Castle JSSE must be configured to use the Bouncy Castle TLS library in FIPS mode. Obtain the Bouncy Castle TLS library from https://www.bouncycastle.org/fips-java/. The jar file should then be installed in a JRE search path.
The following classes must be imported in the application in which the component will be used:
import java.security.Security;
import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider;
//required to use BCJSSE when SSLProvider is set to sslpPlatform
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;
The Bouncy Castle provider must be added as a valid provider and also must be configured to operate in FIPS mode:
System.setProperty("org.bouncycastle.fips.approved_only","true");
Security.addProvider(new BouncyCastleFipsProvider());
//required to use BCJSSE when SSLProvider is set to sslpPlatform
Security.addProvider(new BouncyCastleJsseProvider("fips:BCFIPS"));
//optional - configure logging level of BCJSSE
Logger.getLogger("org.bouncycastle.jsse").setLevel(java.util.logging.Level.OFF);
//configure the class to use BCJSSE
component.setSSLProvider(1); //platform
component.config("UseFIPSCompliantAPI=true");
Note: TLS 1.3 support requires the Bouncy Castle TLS library version 1.0.14 or later.
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 classes 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 article.
Note: Enabling FIPS compliance requires a special license; please contact sales@nsoftware.com for details.
Setting this configuration setting to true tells the class to use the internal implementation instead of using the system security libraries.
This setting is set to false by default on all platforms.
Trappable Errors (EzRand Class)
EzRand Errors
101 | Unsupported algorithm. |
105 | Invalid Min, Max, or RandomBytesLength value. |