Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TotpCalculator.cs
1using System;
2using System.Threading.Tasks;
3
5{
10 public class TotpCalculator
11 {
15 public const int DefaultTimeStepSeconds = 30;
16
20 public const long DefaultT0 = 0;
21
22 private readonly HotpCalculator hotp;
23 private readonly long t0;
24 private readonly int timeStepSeconds;
25
31 public TotpCalculator(byte[] Secret)
32 : this(HotpCalculator.DefaultNrDigits, Secret, HotpCalculator.DefaultHashFunction)
33 {
34 }
35
42 public TotpCalculator(int NrDigits, byte[] Secret)
43 : this(NrDigits, Secret, HotpCalculator.DefaultHashFunction)
44 {
45 }
46
56 {
57 }
58
70 {
71 }
72
83 int TimeStepSeconds, long T0)
84 {
85 CheckTimeStepSeconds(TimeStepSeconds, T0);
86
87 this.timeStepSeconds = TimeStepSeconds;
88 this.t0 = T0;
89 this.hotp = new HotpCalculator(NrDigits, Secret, HashFunction);
90 }
91
92 internal static void CheckTimeStepSeconds(int TimeStepSeconds, long T0)
93 {
94 if (TimeStepSeconds <= 0)
95 {
96 throw new ArgumentOutOfRangeException(nameof(TimeStepSeconds),
97 "Time step must be a positive integer.");
98 }
99
100 if (T0 < 0)
101 {
102 throw new ArgumentOutOfRangeException(nameof(T0),
103 "T0 must be a non-negative integer.");
104 }
105 }
106
111 public static async Task<TotpCalculator> TryCreate(string OtpEndpoint)
112 {
113 ExternalCredential Secret = await ExternalCredential.GetSecret(OtpEndpoint, CredentialAlgorithm.TOTP);
114 if (Secret is null || !Secret.HashFunction.HasValue)
115 return null;
116
117 return new TotpCalculator(Secret.NrDigits, Secret.Secret, Secret.HashFunction.Value,
118 Secret.TimeStepSeconds);
119 }
120
124 public int NrDigits => this.hotp.NrDigits;
125
129 public HashFunction HashFunction => this.hotp.HashFunction;
130
134 public int TimeStepSeconds => this.timeStepSeconds;
135
140 public int Compute()
141 {
142 return this.Compute(DateTime.UtcNow);
143 }
144
150 public int Compute(DateTime Timestamp)
151 {
152 long Counter = CalcCounter(Timestamp, this.timeStepSeconds, this.t0);
153 return this.hotp.Compute(Counter);
154 }
155
159 public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
160
167 public static int Compute(byte[] Secret, DateTime Timestamp)
168 {
169 return Compute(HotpCalculator.DefaultNrDigits, Secret, Timestamp);
170 }
171
179 public static int Compute(int NrDigits, byte[] Secret, DateTime Timestamp)
180 {
181 return Compute(NrDigits, Secret, HotpCalculator.DefaultHashFunction, Timestamp);
182 }
183
192 public static int Compute(int NrDigits, byte[] Secret, HashFunction HashFunction,
193 DateTime Timestamp)
194 {
196 Timestamp, DefaultT0);
197 }
198
209 public static int Compute(int NrDigits, byte[] Secret, HashFunction HashFunction,
210 int TimeStepSeconds, DateTime Timestamp, long T0)
211 {
212 CheckTimeStepSeconds(TimeStepSeconds, T0);
213
214 long Counter = CalcCounter(Timestamp, TimeStepSeconds, T0);
215 return HotpCalculator.Compute(NrDigits, Secret, HashFunction, Counter);
216 }
217
225 public static long CalcCounter(DateTime Timestamp, int TimeStepSeconds, long T0)
226 {
227 return ((long)Timestamp.ToUniversalTime().Subtract(UnixEpoch).TotalSeconds - T0) / TimeStepSeconds;
228 }
229
233 public DateTime NextStep
234 {
235 get
236 {
237 long Counter = CalcCounter(DateTime.UtcNow, this.timeStepSeconds, this.t0);
238 return UnixEpoch.AddSeconds(((Counter + 1) * this.timeStepSeconds) + this.t0);
239 }
240 }
241 }
242}
Contains OTP secret information for an OTP endpoint.
HashFunction? HashFunction
Hash function used for the endpoint.
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.
Definition: Hashes.cs:26