【OPT】Solutions

If you’re specifically looking for number sequence-based authentication codes (like OTPs, often consisting of 6 digits), the following options are most suitable:

1. TOTP (Time-based One-Time Password) using twilio-otp or java-totp

TOTP (Time-based One-Time Password) generates numeric sequences that change periodically (e.g., every 30 seconds). It is a commonly used mechanism for two-factor authentication (2FA), and it generates a sequence of numbers, typically 6 digits.

  • Maven dependency:

    <dependency>
      <groupId>com.warrenstrange</groupId>
      <artifactId>googleauth</artifactId>
      <version>1.4.0</version>
    </dependency>
    
  • Example usage (with java-totp):

    import com.warrenstrange.googleauth.GoogleAuthenticator;
    
    GoogleAuthenticator gAuth = new GoogleAuthenticator();
    int otp = gAuth.getTotpPassword("your-secret-key");
    

    This will generate a 6-digit numeric OTP based on a time-window.

2. HOTP (HMAC-based One-Time Password) using commons-codec

HOTP generates numeric sequences based on a counter (not time-based like TOTP). It is also a number-based sequence and typically results in a 6-digit or longer OTP.

  • Maven dependency:

    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.15</version>
    </dependency>
    
  • Example usage:

    import org.apache.commons.codec.digest.HmacUtils;
    import org.apache.commons.codec.binary.Base32;
    
    byte[] key = new Base32().decode("your-secret-key");
    long counter = 12345L;  // A counter you maintain
    int otp = HmacUtils.hmacSha1(key, counter);
    

    This approach generates a numeric sequence based on a secret key and counter value.

3. Custom Number Generation using SecureRandom

If you just need a random sequence of numbers (e.g., for simple email verification codes), you can use SecureRandom to generate a numeric sequence.

  • Example usage:
    import java.security.SecureRandom;
    
    SecureRandom random = new SecureRandom();
    int otp = 100000 + random.nextInt(900000);  // Generates a 6-digit number
    
    This will generate a 6-digit numeric OTP, which is simple and effective for most email verification needs.

Summary:

  • TOTP and HOTP are ideal if you need more secure, standardized OTPs with time-based or counter-based mechanisms (usually for 2FA).
  • Custom random number generation using SecureRandom is the simplest method for generating a numeric sequence, typically for less security-critical scenarios like email verification.

If you need a secure numeric OTP, TOTP or HOTP is the better choice, especially for systems requiring re-verification over time or multiple verification attempts.

Licensed under CC BY-NC-SA 4.0
Last updated on Sep 23, 2024 00:00 UTC
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy