Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HotpCalculator.cs
1using System;
2using System.Threading.Tasks;
3
5{
10 public class HotpCalculator
11 {
15 public const int DefaultNrDigits = 6;
16
21
22 private readonly int nrDigits;
23 private readonly byte[] secret;
24 private readonly HashFunction hashFunction;
25
31 public HotpCalculator(byte[] Secret)
33 {
34 }
35
42 public HotpCalculator(int NrDigits, byte[] Secret)
43 : this(NrDigits, Secret, DefaultHashFunction)
44 {
45 }
46
55 {
56 CheckNrDigits(NrDigits);
57 CheckSecret(Secret);
58
59 this.nrDigits = NrDigits;
60 this.secret = Secret;
61 this.hashFunction = HashFunction;
62 }
63
64 internal static void CheckNrDigits(int NrDigits)
65 {
66 if (NrDigits < 6)
67 {
68 throw new ArgumentOutOfRangeException(nameof(NrDigits),
69 "Number of digits must be at least 6.");
70 }
71
72 if (NrDigits > 8)
73 {
74 throw new ArgumentOutOfRangeException(nameof(NrDigits),
75 "Number of digits must be at most 8.");
76 }
77 }
78
79 internal static void CheckSecret(byte[] Secret)
80 {
81 if (Secret is null)
82 throw new ArgumentNullException(nameof(Secret));
83
84 if (Secret.Length == 0)
85 throw new ArgumentException("Secret must have a length greater than zero.", nameof(Secret));
86 }
87
91 public int NrDigits => this.nrDigits;
92
96 public HashFunction HashFunction => this.hashFunction;
97
103 public static async Task<HotpCalculator> TryCreate(string OtpEndpoint)
104 {
105 ExternalCredential Secret = await ExternalCredential.GetSecret(OtpEndpoint, CredentialAlgorithm.HOTP);
106 if (Secret is null || !Secret.HashFunction.HasValue)
107 return null;
108
109 return new HotpCalculator(Secret.NrDigits, Secret.Secret, Secret.HashFunction.Value);
110 }
111
117 public int Compute(long Counter)
118 {
119 return Compute(this.nrDigits, this.secret, this.hashFunction, Counter);
120 }
121
128 public static int Compute(byte[] Secret, long Counter)
129 {
130 return Compute(DefaultNrDigits, Secret, Counter);
131 }
132
140 public static int Compute(int NrDigits, byte[] Secret, long Counter)
141 {
142 return Compute(NrDigits, Secret, DefaultHashFunction, Counter);
143 }
144
153 public static int Compute(int NrDigits, byte[] Secret, HashFunction HashFunction,
154 long Counter)
155 {
156 CheckNrDigits(NrDigits);
157 CheckSecret(Secret);
158
159 byte[] Data = new byte[8];
160 int i;
161
162 for (i = 7; i >= 0; i--)
163 {
164 Data[i] = (byte)Counter;
165 Counter >>= 8;
166 }
167
168 byte[] HMAC = Hashes.ComputeHMACHash(HashFunction, Secret, Data);
169 int Offset = HMAC[^1] & 0x0f;
170 int Nr = 0;
171
172 for (i = 0; i < 4; i++)
173 {
174 Nr <<= 8;
175 Nr |= HMAC[Offset++];
176 }
177
178 Nr &= 0x7fffffff;
179
180 int Divisor = (int)Math.Pow(10, NrDigits);
181
182 return Nr % Divisor;
183 }
184 }
185}
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeHMACHash(HashFunction Function, byte[] Key, byte[] Data)
Computes a hash of a block of binary data.
Definition: Hashes.cs:335
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....
HotpCalculator(int NrDigits, byte[] Secret, HashFunction HashFunction)
Implements the HOTP calculator algorithm, as defined in RFC 4226: https://datatracker....
static int Compute(byte[] Secret, long Counter)
Calculates the expected one-time-password for the given counter value.
HotpCalculator(int NrDigits, byte[] Secret)
Implements the HOTP calculator algorithm, as defined in RFC 4226: https://datatracker....
static int Compute(int NrDigits, byte[] Secret, HashFunction HashFunction, long Counter)
Calculates the expected one-time-password for the given counter value.
int Compute(long Counter)
Calculates the expected one-time-password for the given counter value.
const int DefaultNrDigits
Default number of digits (6).
static int Compute(int NrDigits, byte[] Secret, long Counter)
Calculates the expected one-time-password for the given counter value.
HashFunction HashFunction
Hash function to use in computation.
static async Task< HotpCalculator > TryCreate(string OtpEndpoint)
Tries to create an HOTP calculator for the given endpoint.
const HashFunction DefaultHashFunction
Default Hash Function (SHA-1)
int NrDigits
Number of digits to present.
HotpCalculator(byte[] Secret)
Implements the HOTP calculator algorithm, as defined in RFC 4226: https://datatracker....
CredentialAlgorithm
Specifies the type of external credential algorithm to use.
HashFunction
Hash method enumeration.
Definition: Hashes.cs:26