-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.cs
83 lines (79 loc) · 2.61 KB
/
Commands.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace CertDetails
{
abstract class Command
{
public abstract int Run(IEnumerable<string> parameters, ResultWriter resultWriter);
}
class ShowCommand : Command
{
public override int Run(IEnumerable<string> parameters, ResultWriter resultWriter)
{
try
{
if (!parameters.Any())
{
Console.WriteLine("Must at least specify a certificate file.");
return 10;
}
var fileAndPassword = parameters.ToArray();
string file = fileAndPassword[0];
string password = null;
if (fileAndPassword.Length > 1)
{
password = fileAndPassword[1];
}
CertificateDetails details = null;
if (password != null)
{
details = CertificateDetails.OpenFromFile(file, password);
}
else
{
try
{
details = CertificateDetails.OpenFromFile(file);
}
catch (CryptographicException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("file seems to be a .pfx file with a private key, please also specify the password.");
return 1;
}
}
resultWriter.WriteHeader(details);
resultWriter.Write(details);
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
return 1;
}
return 0;
}
}
class RecursiveCommand : Command
{
public override int Run(IEnumerable<string> parameters, ResultWriter resultWriter)
{
string startDirectory = Environment.CurrentDirectory;
string pattern = "*.cer";
bool isFirst = true;
foreach (var file in Directory.EnumerateFiles(startDirectory, pattern, SearchOption.AllDirectories))
{
var details = CertificateDetails.OpenFromFile(file);
if (isFirst)
{
isFirst = false;
resultWriter.WriteHeader(details);
}
resultWriter.Write(details);
}
return 0;
}
}
}