Generating OTP (One Time Password) in Java

This article will show the Java programs for Generating OTP. Nowadays, OTP (One Time Password) is very popular and used for authentication purposes such as login, banking transactions, etc. The OTP verification is usually done by sending a random pin (usually a 4-digit or 6-digit number) to the registered Mobile number or Email-id and that is confirmed by the user with the given OTP.

Here, we will see how to generate such kind of OTP. There are multiple ways to Generate OTP.

  • Generating OTP using Math.random()
  • Generating OTP using java.util.Random

Generate OTP using Math.random()

The Math.random() is a static method that returns a floating-point, pseudo-random number that’s greater than or equal to 0 and less than 1, with approximately uniform distribution over that range. In the below example, we are generating a 4-digits OTP number.

package com.javacodepoint.miscellaneous;
/*
 * Generate OTP using Math.random()
 */
public class GenerateOTP {

	// generate string otp
	public static String generateOTP() {
		// declare randomNo to store the otp
		// generate 4 digits otp
		int randomNo = (int) (Math.random() * 9000) + 1000;
		String otp = String.valueOf(randomNo);
		// return otp
		return otp;
	}

	// main method to test
	public static void main(String[] args) {
		System.out.println("Generate OTP (One Time Password),");
		String otp = generateOTP();
		System.out.println("4 digits OTP: " + otp);
	}

}

OUTPUT:

Generate OTP (One Time Password),
4 digits OTP: 2786

Generate OTP using java.util.Random

The Random class is available under java.util package. This class provides multiple methods to generate random numbers such as nextInt(), nextDouble(), nextLong(), etc.

Generating OTP number in integer

In this example, we are generating the OTPs in an integer format. let’s see the Java code below:

package com.javacodepoint.miscellaneous;

import java.util.Random;

public class OTPGenerator {

	// Generate integer OTP
	public static int generateOTP(int length) {

		int otp = 0;

		// Create object of Random class
		Random random = new Random();

		for (int i = 0; i < length; i++) {
			// Generate a random digit from 0 to 9
			int digit = random.nextInt(10);

			// Concat the generated digit to otp
			otp = otp * 10 + digit;
		}

		// return final otp
		return otp;

	}

	// main method to test
	public static void main(String[] args) {

		// Generate multiple OTPs
		System.out.println("The 4 digits generated OTP is: " + generateOTP(4));
		System.out.println("The 6 digits generated OTP is: " + generateOTP(6));
		System.out.println("The 8 digits generated OTP is: " + generateOTP(8));

	}

}

OUTPUT:

The 4 digits generated OTP is: 1666
The 6 digits generated OTP is: 989077
The 8 digits generated OTP is: 9139337

Generating OTP number in String

package com.javacodepoint.miscellaneous;

import java.util.Random;

public class OTPGenerator2 {

	// Generate string OTP
	public static String generateOTP(int length) {

		String otp = "";

		// Create object of Random class
		Random random = new Random();

		for (int i = 0; i < length; i++) {
			// Generate a random digit from 0 to 9
			int digit = random.nextInt(10);

			// Concatenate the generated digit to otp
			otp = otp + digit;
		}

		// return final otp
		return otp;

	}

	// main method to test
	public static void main(String[] args) {

		// Generate multiple OTPs
		System.out.println("The 4 digits generated OTP is: " + generateOTP(4));
		System.out.println("The 6 digits generated OTP is: " + generateOTP(6));
		System.out.println("The 8 digits generated OTP is: " + generateOTP(8));

	}

}

OUTPUT:

The 4 digits generated OTP is: 5269
The 6 digits generated OTP is: 299249
The 8 digits generated OTP is: 10231093

Continue reading, Generate Random String in Java.

Java logical programs list


Java Basic Programs

Java String Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *