-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
217 lines (182 loc) · 7.63 KB
/
Program.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.CommandLine;
namespace simpleauthenticator
{
internal static class Program
{
static int Main(string[] args)
{
RootCommand rootCommand = new("Generates one time passwords (HOTP and TOTP tokens).")
{
CreateTotpCommand(),
CreateHotpCommand()
};
return rootCommand.Invoke(args);
}
private static Command CreateTotpCommand()
{
var command = new Command(
"totp",
"Generates time based one time password (TOTP token, RFC 6238).");
var secretKeyInBase32Option = new Option<string?>(
name: "--secretkey-base32",
description: "Base32 encoded secret key (whitespaces allowed). Example: 'A5YS 2UP6 K4UF 46GD'.");
secretKeyInBase32Option.AddAlias("-s");
secretKeyInBase32Option.AddAlias("--secretkey");
command.AddOption(secretKeyInBase32Option);
var secretKeyInBase64Option = new Option<string?>(
name: "--secretkey-base64",
description: "Base64 encoded secret key (whitespaces allowed). Example: 'B3Et Uf5X KF54 ww=='.");
secretKeyInBase64Option.AddAlias("-s64");
command.AddOption(secretKeyInBase64Option);
var tokenLengthOption = new Option<int?>(
name: "--token-length",
description: "Token length. Default: 6.");
command.AddOption(tokenLengthOption);
var hmacAlgorithmOption = new Option<HmacAlgorithm?>(
name: "--hmac",
description: "HMAC algorithm to use. Default: SHA1. Allowed values: Md5, Sha1, Sha2_256, Sha2_512, Sha3_256, Sha3_512.");
command.AddOption(hmacAlgorithmOption);
command.SetHandler(
(base32EncodedSecretKey, base64EncodedSecretKey, tokenLength, hmacAlgorithm) =>
{
GenerateTotp(base32EncodedSecretKey, base64EncodedSecretKey, tokenLength, hmacAlgorithm);
},
secretKeyInBase32Option,
secretKeyInBase64Option,
tokenLengthOption,
hmacAlgorithmOption);
return command;
}
private static Command CreateHotpCommand()
{
var command = new Command(
"hotp",
"Generates hmac based one time password (HOTP token, RFC 4226).");
var secretKeyInBase32Option = new Option<string?>(
name: "--secretkey-base32",
description: "Base32 encoded secret key (whitespaces allowed). Example: 'A5YS 2UP6 K4UF 46GD'.");
secretKeyInBase32Option.AddAlias("-s");
secretKeyInBase32Option.AddAlias("--secretkey");
command.AddOption(secretKeyInBase32Option);
var secretKeyInBase64Option = new Option<string?>(
name: "--secretkey-base64",
description: "Base64 encoded secret key (whitespaces allowed). Example: 'B3Et Uf5X KF54 ww=='.");
secretKeyInBase64Option.AddAlias("-s64");
command.AddOption(secretKeyInBase64Option);
var counterOption = new Option<long>(
name: "--counter",
description: @"8-byte counter value, the moving factor. This counter MUST be synchronized between the HOTP generator (client) and the HOTP validator (server).");
counterOption.IsRequired = true;
counterOption.AddAlias("-c");
command.AddOption(counterOption);
var tokenLengthOption = new Option<int?>(
name: "--token-length",
description: "Token length. Default: 6.");
command.AddOption(tokenLengthOption);
var hmacAlgorithmOption = new Option<HmacAlgorithm?>(
name: "--hmac",
description: "HMAC algorithm to use. Default: SHA1. Allowed values: Md5, Sha1, Sha2_256, Sha2_512, Sha3_256, Sha3_512.");
command.AddOption(hmacAlgorithmOption);
command.SetHandler(
(base32EncodedSecretKey, base64EncodedSecretKey, counter, tokenLength, hmacAlgorithm) =>
{
GenerateHotp(base32EncodedSecretKey, base64EncodedSecretKey, counter, tokenLength, hmacAlgorithm);
},
secretKeyInBase32Option,
secretKeyInBase64Option,
counterOption,
tokenLengthOption,
hmacAlgorithmOption);
return command;
}
private static int GenerateTotp(
string? base32EncodedSecretKey,
string? base64EncodedSecretKey,
int? tokenLength,
HmacAlgorithm? hmacAlgorithm)
{
byte[] secretKey;
if (base64EncodedSecretKey != null)
{
try
{
secretKey =
BaseEncodings.FromBase64String(base64EncodedSecretKey);
}
catch (Exception ex)
{
Console.WriteLine("Failed to decode secret key from base64: {0}", ex);
return -1;
}
}
else if (base32EncodedSecretKey != null)
{
try
{
secretKey =
BaseEncodings.FromBase32String(base32EncodedSecretKey);
}
catch (Exception ex)
{
Console.WriteLine("Failed to decode secret key from base32: {0}.", ex);
return -1;
}
}
else
{
Console.WriteLine("Secret key is not specified.");
return -1;
}
var result = Totp.Generate(secretKey, tokenLength ?? 6, hmacAlgorithm ?? HmacAlgorithm.Sha1);
Console.WriteLine(
"Token: {0} (valid for {1} {2}).",
result.Value.ToString("d" + (tokenLength ?? 6).ToString()),
result.LifeTime.TotalSeconds,
result.LifeTime.TotalSeconds > 1 ? "seconds" : "second");
return 0;
}
private static int GenerateHotp(
string? base32EncodedSecretKey,
string? base64EncodedSecretKey,
long counter,
int? tokenLength,
HmacAlgorithm? hmacAlgorithm)
{
byte[] secretKey;
if (base64EncodedSecretKey != null)
{
try
{
secretKey =
BaseEncodings.FromBase64String(base64EncodedSecretKey);
}
catch (Exception ex)
{
Console.WriteLine("Failed to decode secret key from base64: {0}", ex);
return -1;
}
}
else if (base32EncodedSecretKey != null)
{
try
{
secretKey =
BaseEncodings.FromBase32String(base32EncodedSecretKey);
}
catch (Exception ex)
{
Console.WriteLine("Failed to decode secret key from base32: {0}.", ex);
return -1;
}
}
else
{
Console.WriteLine("Secret key is not specified.");
return -1;
}
var token = Hotp.Generate(secretKey, counter, tokenLength ?? 6, hmacAlgorithm ?? HmacAlgorithm.Sha1);
Console.WriteLine("Token: {0}.", token.Value);
return 0;
}
}
}