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.
- Apache (13)
- Build Tools (2)
- Gradle (2)
- Caching (1)
- cpanel (1)
- cURL (1)
- Database (7)
- Hibernate (5)
- Java Core (38)
- Java Script (15)
- Bootstrap (1)
- File Upload (7)
- jQuery (3)
- React (3)
- JEE (13)
- JSON (41)
- GSON (13)
- Jackson 1X (1)
- Jackson 2X (12)
- jsoniter (1)
- Logging (2)
- Apache Commons Logging (1)
- Apache Log4J (1)
- Logback (1)
- SLF4J (1)
- MongoDB (1)
- OS (1)
- Linux (1)
- Security (5)
- Server (4)
- Tomcat (4)
- Service (2)
- Micro (2)
- Spring (46)
- Pattern (2)
- Spring Boot (20)
- Spring Data (4)
- Spring MVC (8)
- Spring REST (13)
- Spring Security (7)
- Testing (11)
- XML (5)
- JDOM XML Parser (1)