Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PemDecoder.cs
1using System;
2using System.Collections.Generic;
3using System.Security.Cryptography.X509Certificates;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
8
10{
15 {
19 public const string ContentType = "application/pem-certificate-chain";
20
24 public PemDecoder()
25 {
26 }
27
31 public string[] ContentTypes => new string[] { ContentType };
32
36 public string[] FileExtensions => new string[] { "pem" };
37
44 public bool Decodes(string ContentType, out Grade Grade)
45 {
47 {
48 Grade = Grade.Excellent;
49 return true;
50 }
51 else
52 {
53 Grade = Grade.NotAtAll;
54 return false;
55 }
56 }
57
68 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
69 {
70 List<X509Certificate2> Certificates = new List<X509Certificate2>();
71 string s = Encoding.ASCII.GetString(Data);
72 int i;
73
74 while ((i = s.IndexOf("-----BEGIN CERTIFICATE-----")) >= 0)
75 {
76 s = s.Substring(i + 27).TrimStart();
77
78 i = s.IndexOf("-----END CERTIFICATE-----");
79 if (i > 0)
80 {
81 byte[] Bin = Convert.FromBase64String(s.Substring(0, i).TrimEnd());
82 s = s.Substring(i + 25).TrimStart();
83
84 X509Certificate2 Certificate = new X509Certificate2(Bin);
85
86 Certificates.Add(Certificate);
87 }
88 }
89
90 return Task.FromResult<object>(Certificates.ToArray());
91 }
92
99 public bool TryGetContentType(string FileExtension, out string ContentType)
100 {
101 if (string.Compare(FileExtension, "pem", true) == 0)
102 {
104 return true;
105 }
106 else
107 {
108 ContentType = null;
109 return false;
110 }
111 }
112
119 public bool TryGetFileExtension(string ContentType, out string FileExtension)
120 {
121 switch (ContentType.ToLower())
122 {
124 FileExtension = "pem";
125 return true;
126
127 default:
128 FileExtension = string.Empty;
129 return false;
130 }
131 }
132
133 }
134}
Decodes certificates encoded using the application/pem-certificate-chain content type.
Definition: PemDecoder.cs:15
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: PemDecoder.cs:44
string[] ContentTypes
Supported content types.
Definition: PemDecoder.cs:31
string[] FileExtensions
Supported file extensions.
Definition: PemDecoder.cs:36
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: PemDecoder.cs:99
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: PemDecoder.cs:119
const string ContentType
application/pem-certificate-chain
Definition: PemDecoder.cs:19
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Definition: PemDecoder.cs:68
PemDecoder()
Decodes certificates encoded using the application/pem-certificate-chain content type.
Definition: PemDecoder.cs:24
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7