Java 8 – Generating One Time Password (OTP)


In this short article, we’ll look at how to generate One Time Password (OTP) in Java 8. In Java 8, we can use SplittableRandom, Supplier<T> and SecureRandom to get a secure random OTP.

Integer.parseInt(otpString) is used to get the integer value of generated OTP string.

1. SplittableRandom

SplittableRandom is a final class available in the package java.util. It was introduced in JDK 8. nextInt(int origin, int bound) method returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).

public static String genrateOTP3(final int lengthOfOTP) {

	StringBuilder generatedOTP = new StringBuilder();
	SplittableRandom splittableRandom = new SplittableRandom();

	for (int i = 0; i < lengthOfOTP; i++) {
			
		int randomNumber = splittableRandom.nextInt(0, 9);
		generatedOTP.append(randomNumber);
	}
	return generatedOTP.toString();
}

2. Supplier<T>

Supplier<T> is a functional interface and available in the package java.util.function. Java 8 lambda expression is used and it’s get() method is used to get the result. It was also introduced in JDK 8.

public static Supplier<String> genrateOTP2(final int lengthOfOTP) {
	return () -> {
		StringBuilder otp = new StringBuilder();
		Random random = new Random();

		for (int i = 0; i < lengthOfOTP; i++) {

			// returns pseudo-random value between 0 and 9
			int randomNumber = random.nextInt(9);
			otp.append(randomNumber);
		}
		return otp.toString();
	};
}

The recommended approach of using Supplier<T> functional interface is without wrapping it into a method. For example:

//...

Supplier<String> otpSupplier = () -> {
	StringBuilder otp = new StringBuilder();
	Random random = new Random();
	for (int i = 0; i < 6; i++) {
		int randomNumber = random.nextInt(9);
		otp.append(randomNumber);
	}
	return otp.toString();
};

otpSupplier.get();

//...

3. SecureRandom

SecureRandom class provides a cryptographically strong random number generator (RNG). It’s available in the package java.security and introduced in JDK 1.7.

public static String genrateOTP1(final int lengthOfOTP) {

	StringBuilder generatedOTP = new StringBuilder();
	SecureRandom secureRandom = new SecureRandom();
	
	try {
	
		secureRandom = SecureRandom.getInstance(secureRandom.getAlgorithm());
		
		for (int i = 0; i < lengthOfOTP; i++) {
			generatedOTP.append(secureRandom.nextInt(9));
		}
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	
	return generatedOTP.toString();
}

Let’s see the complete example.

OTPGenerator.java
package org.websparrow;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.function.Supplier;

public class OTPGenerator {

	public static void main(String[] args) {

		// SecureRandom class
		System.out.println("Radom number 1: " + genrateOTP1(6));

		// Supplier interface
		System.out.println("Radom number 2: " + genrateOTP2(4).get());

		// SplittableRandom class
		System.out.println("Radom number 3: " + genrateOTP3(8));

	}

	public static String genrateOTP3(final int lengthOfOTP) {

		StringBuilder generatedOTP = new StringBuilder();
		SplittableRandom splittableRandom = new SplittableRandom();

		for (int i = 0; i < lengthOfOTP; i++) {

			int randomNumber = splittableRandom.nextInt(0, 9);
			generatedOTP.append(randomNumber);
		}
		return generatedOTP.toString();
	}

	public static Supplier<String> genrateOTP2(final int lengthOfOTP) {
		return () -> {
			StringBuilder otp = new StringBuilder();
			Random random = new Random();

			for (int i = 0; i < lengthOfOTP; i++) {

				// returns pseudo-random value between 0 and 9
				int randomNumber = random.nextInt(9);
				otp.append(randomNumber);
			}
			return otp.toString();
		};
	}

	public static String genrateOTP1(final int lengthOfOTP) {
		StringBuilder generatedOTP = new StringBuilder();
		SecureRandom secureRandom = new SecureRandom();
		try {
			secureRandom = SecureRandom.getInstance(secureRandom.getAlgorithm());
			for (int i = 0; i < lengthOfOTP; i++) {
				generatedOTP.append(secureRandom.nextInt(9));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return generatedOTP.toString();
	}
}

Output

Radom number 1: 862656
Radom number 2: 5440
Radom number 3: 30055017

Integer.parseInt(otpString) is used to get the integer value of generated OTP string.

References

  1. How to generate secure random number in Java
  2. Java 8- SplittableRandom Class
  3. Java 8- Supplier<T> Interface
  4. JDK 1.7- SecureRandom Class

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.