Class SimpleTotpGenerator

java.lang.Object
es.kukenan.smartfi.common.utils.otp.SimpleTotpGenerator

public class SimpleTotpGenerator extends Object
Class to generate a Time-based One-time Password and PUC for bypassing the One-time Password for tests and PUC respectively.

The reference implementation for RFC-6238 is not working correctly with Google Authenticator. Google Authenticator is using an Base32 encoded String as secret key, while the reference implementation accepts a Hex encoded secret key. To be compatible with Google Authenticator the reference implementation is no longer used in this class. For your information: The reference implementation: https://tools.ietf.org/html/rfc6238#appendix-A

To support Base32 encoding, the algorithm from this site is used and is working with Google Authenticator: https://weblogs.java.net/blog/evanx/archive/2012/11/07/google-authenticator-thus-enabled

  • Field Details

    • TOTP_TPUC_LENGTH

      public static final int TOTP_TPUC_LENGTH
      Length.
      See Also:
    • LOWERCASE_LETTER_A

      private static final int LOWERCASE_LETTER_A
      Lowercase letter 'a'.
      See Also:
    • secretKey

      private final String secretKey
      the secret key. This should be a String containing the key and should NOT be Base32 encoded. However: the key added to Google Authenticator SHOULD be Base32 encoded. So plaintext key in this method. Same key encoded with Base32 in Google Authenticator.
    • validTimeInSeconds

      private final int validTimeInSeconds
      Key validation time in seconds. When unsure: use 30 (default for Google Authenticator).
  • Constructor Details

    • SimpleTotpGenerator

      public SimpleTotpGenerator(String secretKey, int validTimeInSeconds)
      Full state constructor.
      Parameters:
      secretKey - the secret key. This should be a String containing the key and should NOT be Base32 encoded. However: the key added to Google Authenticator SHOULD be Base32 encoded. So plaintext key in this method. Same key encoded with Base32 in Google Authenticator.
      validTimeInSeconds - Key validation time in seconds. When unsure: use 30 (default for Google Authenticator).
  • Method Details

    • generateTotp

      public String generateTotp()
      Generates a TOTP based on a secret Key.
      Returns:
      the TOTP of length 6
    • generateTotp

      private String generateTotp(byte[] secret, long timeIndex) throws NoSuchAlgorithmException, InvalidKeyException
      Throws:
      NoSuchAlgorithmException
      InvalidKeyException
    • prefixWithZeroesWhenNeeded

      private String prefixWithZeroesWhenNeeded(String totp)
    • generateTPuc

      public String generateTPuc()
      Generates a TPUC based on a secret key by converting a TOTP (only digits) into a PUC (first 2 and last 2 are letters). All lowercase.
      Returns:
      the Time-based PUC of length 6. Format: LetterLetterDigitDigitLetterLetter. E.g: aa11bb
    • getDigitValue

      private int getDigitValue(String pucInDigits, int characterIndex)
    • getStringValue

      private String getStringValue(int firstDigit)
    • main

      public static void main(String[] args)
      Entrypoint for command line OTP generation.
      Parameters:
      args - arguments.