2using System.Threading.Tasks;
23 private readonly
long t0;
24 private readonly
int timeStepSeconds;
97 "Time step must be a positive integer.");
102 throw new ArgumentOutOfRangeException(nameof(T0),
103 "T0 must be a non-negative integer.");
111 public static async Task<TotpCalculator>
TryCreate(
string OtpEndpoint)
142 return this.
Compute(DateTime.UtcNow);
152 long Counter =
CalcCounter(Timestamp, this.timeStepSeconds, this.t0);
153 return this.hotp.Compute(Counter);
159 public static readonly DateTime
UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
167 public static int Compute(
byte[] Secret, DateTime Timestamp)
237 long Counter =
CalcCounter(DateTime.UtcNow,
this.timeStepSeconds,
this.t0);
238 return UnixEpoch.AddSeconds(((Counter + 1) * this.timeStepSeconds) + this.t0);
Contains OTP secret information for an OTP endpoint.
int TimeStepSeconds
Time step in seconds.
byte[] Secret
Endpoint secret.
HashFunction? HashFunction
Hash function used for the endpoint.
int NrDigits
Number of digits.
Implements the HOTP calculator algorithm, as defined in RFC 4226: https://datatracker....
int Compute(long Counter)
Calculates the expected one-time-password for the given counter value.
const int DefaultNrDigits
Default number of digits (6).
const HashFunction DefaultHashFunction
Default Hash Function (SHA-1)
Implements the TOTP calculator algorithm, as defined in RFC 6238: https://datatracker....
DateTime NextStep
Point in time when the current step (and passcode) ends and the next begins.
int NrDigits
Number of digits to present.
static int Compute(int NrDigits, byte[] Secret, DateTime Timestamp)
Calculates the expected one-time-password for the given counter value.
int TimeStepSeconds
Time step in seconds.
static int Compute(int NrDigits, byte[] Secret, HashFunction HashFunction, DateTime Timestamp)
Calculates the expected one-time-password for the given counter value.
static readonly DateTime UnixEpoch
Unix Date and Time epoch, starting at 1970-01-01T00:00:00Z
TotpCalculator(byte[] Secret)
Implements the TOTP calculator algorithm, as defined in RFC 6238: https://datatracker....
static async Task< TotpCalculator > TryCreate(string OtpEndpoint)
Tries to create a TOTP calculator for the given endpoint.
const long DefaultT0
Default time when starting counting steps (0).
int Compute(DateTime Timestamp)
Calculates the expected one-time-password for the given counter value.
int Compute()
Calculates the expected one-time-password for the given counter value.
static int Compute(int NrDigits, byte[] Secret, HashFunction HashFunction, int TimeStepSeconds, DateTime Timestamp, long T0)
Calculates the expected one-time-password for the given counter value.
TotpCalculator(int NrDigits, byte[] Secret, HashFunction HashFunction, int TimeStepSeconds)
Implements the TOTP calculator algorithm, as defined in RFC 6238: https://datatracker....
static long CalcCounter(DateTime Timestamp, int TimeStepSeconds, long T0)
Calculates the counter number for use with the HOTP algorithm.
TotpCalculator(int NrDigits, byte[] Secret)
Implements the TOTP calculator algorithm, as defined in RFC 6238: https://datatracker....
static int Compute(byte[] Secret, DateTime Timestamp)
Calculates the expected one-time-password for the given counter value.
const int DefaultTimeStepSeconds
Default time-step, in seconds (30).
TotpCalculator(int NrDigits, byte[] Secret, HashFunction HashFunction)
Implements the TOTP calculator algorithm, as defined in RFC 6238: https://datatracker....
TotpCalculator(int NrDigits, byte[] Secret, HashFunction HashFunction, int TimeStepSeconds, long T0)
Implements the TOTP calculator algorithm, as defined in RFC 6238: https://datatracker....
CredentialAlgorithm
Specifies the type of external credential algorithm to use.
HashFunction
Hash method enumeration.