Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate OTP Number with 6 digits

What is an OTP number in a login authentication system? Is there any specific algorithm for generating OTP numbers using java (android). Or is an OTP something like random number? How can this be achieved, with optimization.

like image 572
Android Developer Avatar asked Jun 20 '15 11:06

Android Developer


People also ask

What is a 6 digit OTP number?

One Time Password (OTP) is an additional second factor authentication for financial and sensitive transactions performed on db OnlineBanking. OTP is a six-digit numerical code sent in real time as SMS to your registered mobile number while performing the transaction.

How do you generate a random OTP?

random() function can be used to generate random OTP which is predefined in random library.

Why OTP is 4 or 6 digit number?

Because OTPs back then were 4 digit long, you could remember them easily and hence there was no fuss about it. But that is exactly one of the reasons why the world has switched to 6 digit OTPs, because at the end, security is what matters the most.

Did not receive the 6 digit PIN OTP )?

You might have network connectivity issues. Hence having a good and reliable connection is also vital for receiving OTP. You may also restart your Android phone to have your network connection refreshed on your device. Check with your message permission settings on your mobile and allow SMS access to get the OTP.


2 Answers

Easiest way is to just use DecimalFormat with Random class.

String otp= new DecimalFormat("000000").format(new Random().nextInt(999999));
System.out.println(otp);

Sample Outputs,

002428
445307
409185
989828
794486
213934
like image 152
REMITH Avatar answered Oct 16 '22 06:10

REMITH


Please do not reinvent the wheel - especially in case of security and cryptography. You might end up in a really bad state.

Use algorithms, that the community agreed upon like the HOTP and TOTP algorithm specified by the Open Authentication Iniative. These algorithms are also used by the google authenticater and specified in these RFCs. Read them. They are simple.

https://www.rfc-editor.org/rfc/rfc4226

https://www.rfc-editor.org/rfc/rfc6238

like image 38
cornelinux Avatar answered Oct 16 '22 08:10

cornelinux