Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
250 lines (215 loc) · 7.64 KB
/
Copy pathProgram.cs
File metadata and controls
250 lines (215 loc) · 7.64 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using simpleauthenticator;
RootCommand rootCommand = new("Generates one time passwords (HOTP and TOTP tokens).")
{
CreateTotpCommand(),
CreateHotpCommand()
};
return rootCommand.Parse(args).Invoke();
Command CreateTotpCommand()
{
Option<string?> secretKeyInBase32Option = new("--secretkey-base32", "--secretkey", "-s")
{
Description = "Base32 encoded secret key (whitespaces allowed). Example: 'A5YS 2UP6 K4UF 46GD'."
};
Option<string?> secretKeyInBase64Option = new("--secretkey-base64", "--secretkey64", "-s64")
{
Description = "Base64 encoded secret key (whitespaces allowed, trailing '=' can be omitted). Example: 'B3Et Uf5X KF54 ww=='."
};
Option<int?> timeStepOption = new("--time-step", "--period")
{
Description = "Time step in seconds. Default: 30."
};
Option<int?> tokenLengthOption = new("--token-length", "-l")
{
Description = "Token length. Default: 6."
};
Option<HmacAlgorithm?> hmacAlgorithmOption = new("--hmac", "-m")
{
Description = "HMAC algorithm to use. Default: Sha1. Allowed values: Sha1, Sha2_256, Sha2_512, Sha3_256, Sha3_512."
};
var command = new Command("totp", "Generates time based one time password (TOTP token, RFC 6238).")
{
secretKeyInBase32Option,
secretKeyInBase64Option,
timeStepOption,
tokenLengthOption,
hmacAlgorithmOption
};
command.Validators.Add(
commandResult =>
{
bool hasSecretKeyInBase32 = commandResult.GetResult(secretKeyInBase32Option) is not null;
bool hasSecretKeyInBase64 = commandResult.GetResult(secretKeyInBase64Option) is not null;
if (!hasSecretKeyInBase32 && !hasSecretKeyInBase64)
{
commandResult.AddError($"Option '{secretKeyInBase32Option.Name}' or '{secretKeyInBase64Option.Name}' must be specified.");
}
if (hasSecretKeyInBase32 && hasSecretKeyInBase64)
{
commandResult.AddError($"Options '{secretKeyInBase32Option.Name}' and '{secretKeyInBase64Option.Name}' cannot be used together.");
}
});
command.SetAction(
parseResult =>
{
return GenerateTotp(
parseResult.GetValue(secretKeyInBase32Option),
parseResult.GetValue(secretKeyInBase64Option),
parseResult.GetValue(tokenLengthOption),
parseResult.GetValue(timeStepOption),
parseResult.GetValue(hmacAlgorithmOption));
});
return command;
}
Command CreateHotpCommand()
{
Option<string?> secretKeyInBase32Option = new("--secretkey-base32", "--secretkey", "-s")
{
Description = "Base32 encoded secret key (whitespaces allowed). Example: 'A5YS 2UP6 K4UF 46GD'."
};
Option<string?> secretKeyInBase64Option = new("--secretkey-base64", "--secretkey64", "-s64")
{
Description = "Base64 encoded secret key (whitespaces allowed, trailing '=' can be omitted). Example: 'B3Et Uf5X KF54 ww=='."
};
Option<long> counterOption = new("--counter", "-c")
{
Description = @"8-byte counter value, the moving factor. This counter MUST be synchronized between the HOTP generator (client) and the HOTP validator (server).",
Required = true
};
Option<int?> tokenLengthOption = new("--token-length", "-l")
{
Description = "Token length. Default: 6."
};
Option<HmacAlgorithm?> hmacAlgorithmOption = new("--hmac", "-m")
{
Description = "HMAC algorithm to use. Default: Sha1. Allowed values: Sha1, Sha2_256, Sha2_512, Sha3_256, Sha3_512."
};
var command = new Command("hotp", "Generates hmac based one time password (HOTP token, RFC 4226).")
{
secretKeyInBase32Option,
secretKeyInBase64Option,
counterOption,
tokenLengthOption,
hmacAlgorithmOption
};
command.Validators.Add(
commandResult =>
{
bool hasSecretKeyInBase32 = commandResult.GetResult(secretKeyInBase32Option) is not null;
bool hasSecretKeyInBase64 = commandResult.GetResult(secretKeyInBase64Option) is not null;
if (!hasSecretKeyInBase32 && !hasSecretKeyInBase64)
{
commandResult.AddError($"Option '{secretKeyInBase32Option.Name}' or '{secretKeyInBase64Option.Name}' must be specified.");
}
if (hasSecretKeyInBase32 && hasSecretKeyInBase64)
{
commandResult.AddError($"Options '{secretKeyInBase32Option.Name}' and '{secretKeyInBase64Option.Name}' cannot be used together.");
}
});
command.SetAction(
parseResult =>
{
return GenerateHotp(
parseResult.GetValue(secretKeyInBase32Option),
parseResult.GetValue(secretKeyInBase64Option),
parseResult.GetRequiredValue(counterOption),
parseResult.GetValue(tokenLengthOption),
parseResult.GetValue(hmacAlgorithmOption));
});
return command;
}
byte[] DecodeSecretKey(string? base32EncodedSecretKey, string? base64EncodedSecretKey)
{
byte[] secretKey;
if (base32EncodedSecretKey != null)
{
try
{
secretKey =
BaseEncodings.FromBase32String(base32EncodedSecretKey);
}
catch (Exception ex)
{
throw new Exception("Base32 encoded secret key invalid", ex);
}
}
else if (base64EncodedSecretKey != null)
{
try
{
secretKey =
BaseEncodings.FromBase64String(base64EncodedSecretKey);
}
catch (Exception ex)
{
throw new Exception("Base64 encoded secret key invalid", ex);
}
}
else
{
throw new ArgumentException("Base32 or Base64 encoded key should be specified");
}
return secretKey;
}
int GenerateTotp(
string? base32EncodedSecretKey,
string? base64EncodedSecretKey,
int? tokenLength,
int? timeStepInSeconds,
HmacAlgorithm? hmacAlgorithm)
{
byte[] secretKey;
try
{
secretKey = DecodeSecretKey(base32EncodedSecretKey, base64EncodedSecretKey);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to decode secret key. Reason: {ex.Message}.");
return (int)ExitCodes.Failed;
}
try
{
var token = Totp.Generate(secretKey, tokenLength, timeStepInSeconds, hmacAlgorithm);
Console.WriteLine(
"Token: {0} (valid for {1} {2}).",
token.FormattedValue,
token.LifeTime.TotalSeconds,
token.LifeTime.TotalSeconds > 1 ? "seconds" : "second");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to generate Totp token. Reason: {ex.Message}.");
return (int)ExitCodes.Failed;
}
return (int)ExitCodes.Success;
}
int GenerateHotp(
string? base32EncodedSecretKey,
string? base64EncodedSecretKey,
long counter,
int? tokenLength,
HmacAlgorithm? hmacAlgorithm)
{
byte[] secretKey;
try
{
secretKey = DecodeSecretKey(base32EncodedSecretKey, base64EncodedSecretKey);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to decode secret key. Reason: {ex.Message}.");
return (int)ExitCodes.Failed;
}
try
{
var token = Hotp.Generate(secretKey, counter, tokenLength, hmacAlgorithm);
Console.WriteLine("Token: {0}.", token.FormattedValue);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to generate Hotp token. Reason: {ex.Message}.");
return (int)ExitCodes.Failed;
}
return (int)ExitCodes.Success;
}