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;
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 => contentTypes;
32
33 private static readonly string[] contentTypes = new string[] { ContentType };
34
38 public string[] FileExtensions => new string[] { "pem" };
39
46 public bool Decodes(string ContentType, out Grade Grade)
47 {
49 {
50 Grade = Grade.Excellent;
51 return true;
52 }
53 else
54 {
55 Grade = Grade.NotAtAll;
56 return false;
57 }
58 }
59
70 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
71 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
72 {
73 List<X509Certificate2> Certificates = new List<X509Certificate2>();
74 string s = Encoding.ASCII.GetString(Data);
75 int i;
76
77 while ((i = s.IndexOf("-----BEGIN CERTIFICATE-----")) >= 0)
78 {
79 s = s.Substring(i + 27).TrimStart();
80
81 i = s.IndexOf("-----END CERTIFICATE-----");
82 if (i > 0)
83 {
84 byte[] Bin = Convert.FromBase64String(s.Substring(0, i).TrimEnd());
85 s = s.Substring(i + 25).TrimStart();
86
87 X509Certificate2 Certificate = new X509Certificate2(Bin);
88
89 Certificates.Add(Certificate);
90 }
91 }
92
93 return Task.FromResult(new ContentResponse(ContentType, Certificates.ToArray(), Data));
94 }
95
102 public bool TryGetContentType(string FileExtension, out string ContentType)
103 {
104 if (string.Compare(FileExtension, "pem", true) == 0)
105 {
107 return true;
108 }
109 else
110 {
111 ContentType = null;
112 return false;
113 }
114 }
115
122 public bool TryGetFileExtension(string ContentType, out string FileExtension)
123 {
124 switch (ContentType.ToLower())
125 {
127 FileExtension = "pem";
128 return true;
129
130 default:
131 FileExtension = string.Empty;
132 return false;
133 }
134 }
135
136 }
137}
Contains information about a response to a content request.
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:46
string[] ContentTypes
Supported content types.
Definition: PemDecoder.cs:31
string[] FileExtensions
Supported file extensions.
Definition: PemDecoder.cs:38
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: PemDecoder.cs:102
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: PemDecoder.cs:70
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: PemDecoder.cs:122
const string ContentType
application/pem-certificate-chain
Definition: PemDecoder.cs:19
PemDecoder()
Decodes certificates encoded using the application/pem-certificate-chain content type.
Definition: PemDecoder.cs:24
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7