Class TotpCounter

java.lang.Object
tech.mgl.utils.security.gen.TotpCounter

public class TotpCounter extends Object
Counter whose value is a deterministic function of time as described in RFC 6238 "TOTP: Time-Based One-Time Password Algorithm".

The 64-bit counter assumes the value 0 at a predefined point in time and periodically increments its value by one periodically.

The value V of the counter at time instant T is:

 V = (T - T0) / TimeStep
 
where T0 is the earliest time instant at which the counter assumes the value 0, and TimeStep is the duration of time for which the values of the counter remain constant.

Note: All time instants are in seconds since UNIX epoch, and all time durations are in seconds.

Note: All time instants must be non-negative.

Thread-safety: Instances of this class are immutable and are thus thread-safe.

Author:
klyubin@google.com (Alex Klyubin)
  • Constructor Summary

    Constructors
    Constructor
    Description
    TotpCounter(long timeStep)
    Constructs a new TotpCounter that starts with the value 0 at time instant 0 (seconds since UNIX epoch) and increments its value with the specified frequency.
    TotpCounter(long timeStep, long startTime)
    Constructs a new TotpCounter that starts with the value 0 at the specified time and increments its value with the specified frequency.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Gets the earliest time instant at which this counter assumes the value 0.
    long
    Gets the frequency with which the value of this counter changes.
    long
    getValueAtTime(long time)
    Gets the value of this counter at the specified time.
    long
    getValueStartTime(long value)
    Gets the time when the counter assumes the specified value.

    Methods inherited from class java.lang.Object

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

    • TotpCounter

      public TotpCounter(long timeStep)
      Constructs a new TotpCounter that starts with the value 0 at time instant 0 (seconds since UNIX epoch) and increments its value with the specified frequency.
      Parameters:
      timeStep - interval of time (seconds) between successive changes of this counter's value.
    • TotpCounter

      public TotpCounter(long timeStep, long startTime)
      Constructs a new TotpCounter that starts with the value 0 at the specified time and increments its value with the specified frequency.
      Parameters:
      timeStep - interval of time (seconds) between successive changes of this counter's value.
      startTime - the earliest time instant (seconds since UNIX epoch) at which this counter assumes the value 0.
  • Method Details

    • getTimeStep

      public long getTimeStep()
      Gets the frequency with which the value of this counter changes.
      Returns:
      interval of time (seconds) between successive changes of this counter's value.
    • getStartTime

      public long getStartTime()
      Gets the earliest time instant at which this counter assumes the value 0.
      Returns:
      time (seconds since UNIX epoch).
    • getValueAtTime

      public long getValueAtTime(long time)
      Gets the value of this counter at the specified time.
      Parameters:
      time - time instant (seconds since UNIX epoch) for which to obtain the value.
      Returns:
      value of the counter at the time.
    • getValueStartTime

      public long getValueStartTime(long value)
      Gets the time when the counter assumes the specified value.
      Parameters:
      value - value.
      Returns:
      earliest time instant (seconds since UNIX epoch) when the counter assumes the value.