Skip to content

Commit 95f5441

Browse files
committed
Add Import method to CertifficateUtilities
1 parent 72891bf commit 95f5441

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/EasySign.CommandLine/CertificateUtilities.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,49 @@ public static void DisplayCertificate(params X509Certificate2[] certificates)
9090
AnsiConsole.WriteLine();
9191
}
9292

93+
/// <summary>
94+
/// Imports a PEM or DER encoded certificate from a file.
95+
/// </summary>
96+
/// <param name="filePath">
97+
/// The path to the certificate file.
98+
/// </param>
99+
/// <returns>
100+
/// An X509Certificate2 object representing the imported certificate.
101+
/// </returns>
102+
public static X509Certificate2 Import(string filePath)
103+
{
104+
byte[] buffer;
105+
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
106+
{
107+
buffer = new byte[fs.Length];
108+
fs.Read(buffer, 0, buffer.Length);
109+
}
110+
111+
return Import(buffer);
112+
}
113+
114+
/// <summary>
115+
/// Imports a PEM or DER encoded certificate from a byte array.
116+
/// </summary>
117+
/// <param name="data">
118+
/// The byte array containing the certificate data.
119+
/// </param>
120+
/// <returns>
121+
/// An X509Certificate2 object representing the imported certificate.
122+
/// </returns>
123+
public static X509Certificate2 Import(byte[] data)
124+
{
125+
X509Certificate2 certificate;
126+
127+
#if NET9_0_OR_GREATER
128+
certificate = X509CertificateLoader.LoadCertificate(data);
129+
#else
130+
certificate = new X509Certificate2(data);
131+
#endif
132+
133+
return certificate;
134+
}
135+
93136
/// <summary>
94137
/// Imports a PFX file and returns a collection of certificates.
95138
/// </summary>

0 commit comments

Comments
 (0)