-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (56 loc) · 1.72 KB
/
Program.cs
File metadata and controls
61 lines (56 loc) · 1.72 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
namespace GenerateRSAKey
{
internal class Program
{
private const string UsageMessage = "Usage: GenerateRSAKey [\"2048\" or \"4096\" default value 2048> [optional: \"-s\" save pem and jwks.json file]";
private const string GenerateRsaKeyDescription = "Generates an RSA key pair and outputs the private key in PEM format and the JWKS in JSON format.";
static void Main(string[] args)
{
Console.WriteLine($"{GenerateRsaKeyDescription}\n\n");
var (keySize, saveInFile) = ParseArguments(args);
Console.WriteLine("Generate RSA KEY");
KeyGenerator.GenerateRsaKeyAndJwks(keySize, saveInFile);
Console.WriteLine("Key generated");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
private static (KeySize keySize, bool saveInFile) ParseArguments(string[] args)
{
KeySize keySize = KeySize._2048; // Default key size
bool saveInFile = false;
if (args.Length == 1 && (args[0] == "h" || args[0] == "-h"))
{
Console.WriteLine(UsageMessage);
Environment.Exit(0);
}
if (args.Length > 2)
{
throw new ArgumentException("Too many arguments. Usage: GenerateRSAKey [\"2048\" or \"4096\" default value 2048> [optional: \"-s\" save pem and jkws.json file]");
}
if (args.Length == 0)
{
return (keySize, saveInFile);
}
foreach (var arg in args)
{
if (arg == "4096")
{
keySize = KeySize._4096;
}
else if (arg == "2048")
{
keySize = KeySize._2048;
}
else if (arg == "-s")
{
saveInFile = true;
}
else
{
throw new ArgumentException($"Invalid argument: {arg}. Use '2048', '4096' or '-s'.");
}
}
return (keySize, saveInFile);
}
}
}