java.lang.Object
com.irurueta.authserver.authorization.domain.totp.TotpAlgorithm

public class TotpAlgorithm extends Object
The time-based one-time password algorithm of RFC 6238 over HMAC-SHA1, with the parameters every authenticator application supports: 30 second steps and 6 digits. It is pure and holds no state.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Name of the algorithm as written in an otpauth URI.
    static final int
    Number of digits of a code.
    private static final String
    Name of the JCA MAC algorithm.
    private static final int
    Modulus that keeps the last DIGITS decimal digits.
    private static final int
    Mask of the low nibble of the last hash byte, which gives the truncation offset.
    static final long
    Length of a time step, in seconds.
    private static final int
    Mask that clears the sign bit of the truncated word.
    private static final int
    Number of bytes of the truncated word.
    static final int
    Number of steps accepted on each side of the current one, to tolerate clock drift.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    generate(byte[] secret, long step)
    Computes the code of a time step.
    boolean
    matches(byte[] secret, String code, Instant now, long lastUsedStep)
    Tells whether a code is valid, with the window and replay rules of matchingStep(byte[], java.lang.String, java.time.Instant, long).
    matchingStep(byte[] secret, String code, Instant now, long lastUsedStep)
    Finds the time step a presented code corresponds to, accepting WINDOW steps on each side of the current one and rejecting any step that is not newer than the last one used, so that a code cannot be replayed.
    long
    stepAt(Instant instant)
    Computes the time step that contains an instant.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PERIOD_SECONDS

      public static final long PERIOD_SECONDS
      Length of a time step, in seconds.
      See Also:
    • DIGITS

      public static final int DIGITS
      Number of digits of a code.
      See Also:
    • WINDOW

      public static final int WINDOW
      Number of steps accepted on each side of the current one, to tolerate clock drift.
      See Also:
    • ALGORITHM_NAME

      public static final String ALGORITHM_NAME
      Name of the algorithm as written in an otpauth URI.
      See Also:
    • MAC_ALGORITHM

      private static final String MAC_ALGORITHM
      Name of the JCA MAC algorithm.
      See Also:
    • MODULUS

      private static final int MODULUS
      Modulus that keeps the last DIGITS decimal digits.
      See Also:
    • OFFSET_MASK

      private static final int OFFSET_MASK
      Mask of the low nibble of the last hash byte, which gives the truncation offset.
      See Also:
    • SIGN_MASK

      private static final int SIGN_MASK
      Mask that clears the sign bit of the truncated word.
      See Also:
    • TRUNCATED_BYTES

      private static final int TRUNCATED_BYTES
      Number of bytes of the truncated word.
      See Also:
  • Constructor Details

    • TotpAlgorithm

      public TotpAlgorithm()
  • Method Details

    • generate

      public String generate(byte[] secret, long step)
      Computes the code of a time step.
      Parameters:
      secret - the shared secret.
      step - the time step, that is the Unix time divided by PERIOD_SECONDS.
      Returns:
      the code, always DIGITS digits long, left padded with zeros.
      Throws:
      IllegalArgumentException - if the secret is empty.
      IllegalStateException - if the platform has no HMAC-SHA1.
    • stepAt

      public long stepAt(Instant instant)
      Computes the time step that contains an instant.
      Parameters:
      instant - the instant.
      Returns:
      the step.
    • matchingStep

      public OptionalLong matchingStep(byte[] secret, String code, Instant now, long lastUsedStep)
      Finds the time step a presented code corresponds to, accepting WINDOW steps on each side of the current one and rejecting any step that is not newer than the last one used, so that a code cannot be replayed.
      Parameters:
      secret - the shared secret.
      code - the code presented by the user.
      now - current instant.
      lastUsedStep - the step of the last accepted code, or TotpCredential.NEVER_USED.
      Returns:
      the matching step, or an empty value when the code is wrong, outside the window or already used.
    • matches

      public boolean matches(byte[] secret, String code, Instant now, long lastUsedStep)
      Tells whether a code is valid, with the window and replay rules of matchingStep(byte[], java.lang.String, java.time.Instant, long).
      Parameters:
      secret - the shared secret.
      code - the code presented by the user.
      now - current instant.
      lastUsedStep - the step of the last accepted code, or TotpCredential.NEVER_USED.
      Returns:
      true when the code is accepted.