2using System.Threading.Tasks;
22 private readonly
int nrDigits;
23 private readonly
byte[] secret;
64 internal static void CheckNrDigits(
int NrDigits)
68 throw new ArgumentOutOfRangeException(nameof(
NrDigits),
69 "Number of digits must be at least 6.");
74 throw new ArgumentOutOfRangeException(nameof(
NrDigits),
75 "Number of digits must be at most 8.");
79 internal static void CheckSecret(
byte[] Secret)
82 throw new ArgumentNullException(nameof(Secret));
84 if (Secret.Length == 0)
85 throw new ArgumentException(
"Secret must have a length greater than zero.", nameof(Secret));
103 public static async Task<HotpCalculator>
TryCreate(
string OtpEndpoint)
119 return Compute(this.nrDigits, this.secret, this.hashFunction, Counter);
128 public static int Compute(
byte[] Secret,
long Counter)
159 byte[] Data =
new byte[8];
162 for (i = 7; i >= 0; i--)
164 Data[i] = (byte)Counter;
169 int Offset = HMAC[^1] & 0x0f;
172 for (i = 0; i < 4; i++)
175 Nr |= HMAC[Offset++];
180 int Divisor = (int)Math.Pow(10,
NrDigits);
Contains methods for simple hash calculations.
static byte[] ComputeHMACHash(HashFunction Function, byte[] Key, byte[] Data)
Computes a hash of a block of binary data.
Contains OTP secret information for an OTP endpoint.
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....
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.