Package tech.mgl.utils.security.gen
Class TotpCounter
java.lang.Object
tech.mgl.utils.security.gen.TotpCounter
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) / TimeStepwhere
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
ConstructorsConstructorDescriptionTotpCounter(long timeStep) Constructs a newTotpCounterthat starts with the value0at time instant0(seconds since UNIX epoch) and increments its value with the specified frequency.TotpCounter(long timeStep, long startTime) Constructs a newTotpCounterthat starts with the value0at the specified time and increments its value with the specified frequency. -
Method Summary
Modifier and TypeMethodDescriptionlongGets the earliest time instant at which this counter assumes the value0.longGets the frequency with which the value of this counter changes.longgetValueAtTime(long time) Gets the value of this counter at the specified time.longgetValueStartTime(long value) Gets the time when the counter assumes the specified value.
-
Constructor Details
-
TotpCounter
public TotpCounter(long timeStep) Constructs a newTotpCounterthat starts with the value0at time instant0(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 newTotpCounterthat starts with the value0at 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 value0.
-
-
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 value0.- 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.
-