blob: 56f1319037ecb5ef462ad9e24e8bcffd07ba3bb0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoStepsAuthenticator
{
/// <summary>
/// Implementation of RFC 4226 Counter-Based One-Time Password Algorithm
/// </summary>
public class CounterAuthenticator : Authenticator
{
private readonly int WindowSize;
public CounterAuthenticator(int windowSize = 10) {
if (windowSize <= 0) {
throw new ArgumentException("look-ahead window size must be positive");
}
this.WindowSize = windowSize;
}
/// <summary>
/// Generates One-Time-Password.
/// </summary>
/// <param name="secret">Shared Secret</param>
/// <param name="counter">Current Counter</param>
/// <returns>OTP</returns>
public string GetCode(string secret, ulong counter)
{
return GetCodeInternal(secret, counter);
}
/// <summary>
/// Checks if the passed code is valid.
/// </summary>
/// <param name="secret">Shared Secret</param>
/// <param name="code">OTP</param>
/// <param name="counter">Current Counter Position</param>
/// <returns>true if any code from counter to counter + WindowSize matches</returns>
public bool CheckCode(string secret, string code, ulong counter)
{
ulong successfulSequenceNumber = 0uL;
return CheckCode(secret, code, counter, out successfulSequenceNumber);
}
/// <summary>
/// Checks if the passed code is valid.
/// </summary>
/// <param name="secret">Shared Secret</param>
/// <param name="code">OTP</param>
/// <param name="counter">Current Counter Position</param>
/// <param name="usedCounter">Matching counter value if successful</param>
/// <returns>true if any code from counter to counter + WindowSize matches</returns>
public bool CheckCode(string secret, string code, ulong counter, out ulong usedCounter)
{
var codeMatch = false;
ulong successfulSequenceNumber = 0uL;
for (uint i = 0; i <= WindowSize; i++)
{
ulong checkCounter = counter + i;
if (ConstantTimeEquals(GetCode(secret, checkCounter), code)) {
codeMatch = true;
successfulSequenceNumber = checkCounter;
}
}
usedCounter = successfulSequenceNumber;
return codeMatch;
}
}
}
|