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.
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.
random() function can be used to generate random OTP which is predefined in random library.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With