Changing the totp length

Under OTP, I showed with example how to generate an totp.

By default the length of the totp generated will be 6.

We can change that to any number between 6 and 8.

In this post under OTP, I will show with example how to change totp length to 8.

Below is the complete main code for your reference.

Main Class

1  package defaultPackage;
2
3 import com.bastiaanjansen.otp.SecretGenerator;
4 import com.bastiaanjansen.otp.TOTP;
5
6 public class Example8 {
7 public static void main(String[] args) {
8 byte[] secret = SecretGenerator.generate();
9 TOTP.Builder builder = new TOTP.Builder(secret);
10 builder.withPasswordLength(8);
11 TOTP totp = builder.build();
12 System.out.println(totp.now());
13 }
14 }

In the above code, at line 9 we are creating an instance of “TOTP.Builder” class named “builder”.

At line 10, we are calling “withPasswordLength” method on the builder instance and passing 8 as an argument.

In this way we are changing the default otp length from 6 to 8 digits.

Leave a comment