-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPem.cs
More file actions
54 lines (49 loc) · 1.65 KB
/
Pem.cs
File metadata and controls
54 lines (49 loc) · 1.65 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
#region
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
#endregion
namespace Ultz.Extensions.PrivacyEnhancedMail
{
/// <summary>
/// A class containing PEM format helper methods
/// </summary>
public static class Pem
{
/// <summary>
/// Gets a <see cref="X509Certificate2" /> from a Base64-encoded certificate.
/// </summary>
/// <param name="cert"></param>
/// <returns></returns>
public static X509Certificate2 GetCertificate(string cert)
{
return new X509Certificate2(PemExt.GetBytesFromPem(cert, "CERTIFICATE"));
}
#if NET472 || NETCOREAPP2_0 || NETSTANDARD2_1
/// <summary>
/// Gets a <see cref="X509Certificate2" /> from a Base64-encoded certificate and private key.
/// </summary>
/// <param name="cert"></param>
/// <param name="key"></param>
/// <returns></returns>
public static X509Certificate2 GetCertificate(string cert, string key)
{
return GetCertificate(cert)
.CopyWithPrivateKey
(
PemExt.DecodeRsaPrivateKey
(
PemExt.GetBytesFromPem
(
key,
"RSA PRIVATE KEY"
)
)
);
}
#endif
public static (X509Certificate2, RSA) GetCertificateAndKey(string cert, string key)
{
return (GetCertificate(cert), PemExt.DecodeRsaPrivateKey(PemExt.GetBytesFromPem(key, "RSA PRIVATE KEY")));
}
}
}