Presenting the SecureRandomStringGenerator

SecureRandomStringGenerator – an Open Source utility for generating secure random strings

Last week, I wrote a bit about the java.util.Random and and java.security.SecureRandom classes.  Since then I’ve spent some time creating a small utility for using java.security.SecureRandom to generate random alphanumeric strings.  I’ve decided to open source it under the Gnu Public License version 3.  It’s available on GitHub.

The application can be built and run from the command-line or added to an existing project and used as a library.   There are both Gradle and Maven options for building.

When running from the command line the following parameters are accepted.

  • length_of_string: a positive whole number indicating the length of string to generate
  • characters_to_use (optional): a string containing the characters to use in constructing the string
    • If not supplied, the default is case sensitive alphanumeric.
  • -h or –help: displays the usage information

Build with Gradle

From the command line, go to the directory containing the application.

>gradlew clean build

To Run

>java -jar .\build\libs\SecureRandomStringGenerator-1.0.0.jar 64 ABCDEFG1234567890

Example Output

>Generated String: G1A3553C8E6132C3G0B45F4CD2632DAF4FA8F1A5EDCE39DE2B228BB24EDE4EAF

Build with Maven

From the command line, go to the directory containing the application.

>mvn clean package

To Run

>java -jar .\target\SecureRandomGenerator-1.0.0.jar 64 ABCDEFG1234567890

Example Output

>Generated String: 5B9GADAG7A4501E26E4G285139G5EBF1FAFBGDEA7B67AAG73F012GA60B1FC4FD

Use in an Application

To use the SecureRandomString class in an application, build the jar file with Gradle or Maven and add it to your project.

The SecureRandomString class has three constructors:

  • Default Constructor: Creates a default instance of SecureRandomString.  By default, the characters used are all upper and lower case letters and digits.
  • SecureRandomString(char[] allowedChars):  Provide a character array of characters you want to be in the generated string.
  • SecureRandomString(String allowedCharsString): Provide a string containing all the characters you want to be in the generated string.

Examples:

int length = 24; 

//Default constructor 
SecureRandomString defaultSrs = new SecureRandomString(); 
String alphanumericString = defaultSrs.generateSecureRandomString(length); 
System.out.println("Alphanumeric Secure Random String: " + alphanumericString); 

//Char Array Constructor 
char[] allowedChars = {'a', 'b', 'c', 'e', 'd', 'f', 'g', '!', '@', '$'}; 
SecureRandomString charArraySrs = new SecureRandomString(allowedChars); 
String charArrayString = charArraySrs.generateSecureRandomString(length); 
System.out.println("Char Array Secure Random String: " + charArrayString); 

//String Constructor 
String allowedCharsString = "ABCDEFG1234567890"; 
SecureRandomString stringSrs = new SecureRandomString(allowedCharsString); 
String stringRandomString = stringSrs.generateSecureRandomString(length); 
System.out.println("String Secure Random String: " + stringRandomString);

Example output:

Alphanumeric Secure Random String: dClcmSFIhzx68OiuItdXjdO5
Char Array Secure Random String: a$@g$a!!gaeg$g@dbfad@!eb
String Secure Random String: E1930C37220A91B0D3AC51G7

I enjoyed writing this utility and I hope some of you will find it useful.  I’ve already thought of something I should have included, so stay tuned for an update.  Please feel free to leave a comment with any questions or any problems you encounter.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s