Generate OTP


Generate OTP - TimeBasedOneTimePasswordGenerator (like Google Authentication)


code for generate OTP:-
------------------------------------------------------------------------------------------------------

String currentOTP;

public void genereateOTP(String key) {

    try {

        byte[] secret1 = new Base32().decode(key);

        //Key key = new SecretKeySpec(secret1,0,secret1.length, "DES");
        SecretKey key = new SecretKeySpec(secret1, 0, secret1.length, "AES");

        TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
        Date now = new Date();
        Date later = new Date(now.getTime() + TimeUnit.SECONDS.toMillis(timeslice));

        System.out.format("Current password: %06d\n", totp.generateOneTimePassword(key, now));
        System.out.format("Future password:  %06d\n", totp.generateOneTimePassword(key, later));

       int generatedOTP = totp.generateOneTimePassword(key, now);

        currentOTP = String.format("%06d", generatedOTP);

    } catch (Exception e) {
        e.printStackTrace();
    }

}





Download Classes Here:-
https://drive.google.com/open?id=13Bw7F2ZaOv0y-IkSgqfD9N_FGfRKECaZ

No comments:

Post a Comment

Run/install/debug Android applications over Wi-Fi ?

Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...