Saturday 9 February 2019

JAVA : How to generate OTP


OtpExample.java
package java8s;

import java.util.*;

public class OtpExample {
    
     static char[] show(int len)
    { 
        System.out.print("Your OTP is : ");
 
        String numbers = "0123456789";
 
        Random rd = new Random();
 
        char[] otp = new char[len];
 
        for (int i = 0; i < len; i++)
        {
            
            otp[i] =
             numbers.charAt(rd.nextInt(numbers.length()));
        }
        return otp;
    }

     public static void main(String[] args) {
          
           int length = 5;
        System.out.println(show(length));

     }

}

Output:
Your OTP is : 35838

Thanks & Regards:
Trilochan Tarai
SILAN Technology & java8s.com
+91-9658317757

JAVA : How to generate password


//JAVA source code to generate random password
PasswordExample.java
package java8s;

import java.util.*;
public class PasswordExample {

     public static void main(String[] args) {
          
           int length = 10;
        System.out.println(silanPassword(length));
          

     }
    
     static char[] silanPassword(int len)
    {
        System.out.print("Your new password is : ");
 
       
        String uchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lchars = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        String symbols = "!@#$%^&*_=+-/.?<>)";
 
 
        String values = uchars + lchars +
                        numbers + symbols;
 
        
        Random rd = new Random();
 
        char[] password = new char[len];
 
        for (int i = 0; i < len; i++)
        {
           
            password[i] =
              values.charAt(rd.nextInt(values.length()));
 
        }
        return password;
    }

}
Output:
Your new password is : mR!XPdfVmg

Note : Here we have used random() to generate the password. It may change every time due to use of random() method.

Thanks & Regards:'
Trilochan Tarai
SILAN Technology & java8s.com
0674-2361252