Posted In: Java Core, Security

How to generate a random alpha-numeric string

Creating a random string with A-Z and 0-9

Techniques used

1. Apache Commons RandomStringUtils
2. SecureRandom and com.google.common.io.BaseEncoding
3. UUID.randomUUID()
4. https://www.random.org/strings/

In all these methods if you are using these strings for security and do not to take any chance with uniqueness then I suggest store it in DB with unique constraint or add a sequence to generated string.

1. Using Apache Commons RandomStringUtils

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.5</version>
</dependency>

package com.example.random;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

public class Generator {

	@Test
	public void generate1() throws Exception {
		System.out.println("\ngenerate1() start");
		Set set = new HashSet();
		for (int i = 0; i < 20000; i++) {
			set.add(RandomStringUtils.randomAlphanumeric(16));
		}
		System.out.println("\ngenerate1() set.size()=" + set.size());
	}

	@Test
	public void generate2() throws Exception {
		System.out.println("\ngenerate2() start");
		Set set = new HashSet();
		for (int i = 0; i < 20000; i++) {
			set.add(RandomStringUtils.randomAlphabetic(16));
		}
		System.out.println("\ngenerate2() set.size()=" + set.size());
	}

	@Test
	public void generate3() throws Exception {
		System.out.println("\ngenerate3() start");
		Set set = new HashSet();
		for (int i = 0; i < 20000; i++) {
			set.add(RandomStringUtils.random(16));
		}
		System.out.println("\ngenerate3() set.size()=" + set.size());
	}

	@Test
	public void generate4() throws Exception {
		System.out.println("\ngenerate4() start");
		Set set = new HashSet();
		for (int i = 0; i < 20000; i++) {
			set.add(RandomStringUtils.randomNumeric(16));
		}
		System.out.println("\ngenerate3() set.size()=" + set.size());
	}
}

2. Using SecureRandom and com.google.common.io.BaseEncoding

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>23.0</version>
</dependency>

package com.example.random;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

import com.google.common.io.BaseEncoding;

public class Generator2 {

	@Test
	public void generate1() throws Exception {
		System.out.println("\ngenerate1() start");
		Set set = new HashSet();
		for (int i = 0; i < 20000; i++) {
			SecureRandom random = new SecureRandom();
			byte[] values = new byte[16];
			random.nextBytes(values);
			set.add(BaseEncoding.base64Url().omitPadding().encode(values));
		}
		System.out.println("\ngenerate1() set.size()=" + set.size());
	}
}

3. Using UUID.randomUUID()


	@Test
	public void generate2() throws Exception {
		System.out.println("\ngenerate2() start");
		Set set = new HashSet();
		for (int i = 0; i < 20000; i++) {
			set.add(UUID.randomUUID().toString());
		}
		System.out.println("\ngenerate1() set.size()=" + set.size());
	}

4. Using https://www.random.org/strings/

You could use this website to generate and download random strings. Then store this strings in a DB with a unique constraint column.Fetch the each row to get a unique random key.

by , on August 30th, 2017

  • Categories