Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetAlgorithms.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
6using Waher.Events;
12using Waher.Script;
14
16{
21 {
26 : base("Crypto/GetAlgorithms",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetAlgorithms).Namespace + ".JSON.GetAlgorithms.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetAlgorithms).Namespace + ".XML.GetAlgorithms.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 List<NamedDictionary<string, object>> Algorithms = new List<NamedDictionary<string, object>>();
48
49 lock (endpoints)
50 {
51 foreach (EllipticCurveEndpoint Endpoint in endpoints.Values)
52 {
53 Algorithms.Add(new NamedDictionary<string, object>("Algorithm", AgentNamespace)
54 {
55 { "localName", Endpoint.LocalName },
56 { "namespace", Endpoint.Namespace },
57 { "securityStrength", Endpoint.SecurityStrength },
58 { "safe", Endpoint.Safe },
59 { "slow", Endpoint.Slow },
60 { "pqc", Endpoint.PostQuantumCryptography },
61 { "score", Endpoint.Score }
62 });
63 }
64 }
65
66 await Response.Return(new NamedDictionary<string, object>("AlgorithmsResult", AgentNamespace)
67 {
68 { "Algorithms", Algorithms.ToArray() }
69 });
70 }
71
72 private readonly static Dictionary<string, EllipticCurveEndpoint> endpoints = new Dictionary<string, EllipticCurveEndpoint>();
73
74 static GetAlgorithms()
75 {
76 GetEndpointsLocked();
77
78 Types.OnInvalidated += (Sender, e) =>
79 {
80 lock (endpoints)
81 {
82 endpoints.Clear();
83 GetEndpointsLocked();
84 }
85 };
86 }
87
88 private static void GetEndpointsLocked()
89 {
90 foreach (Type T in Types.GetTypesImplementingInterface(typeof(IE2eEndpoint)))
91 {
92 if (T.IsAbstract)
93 continue;
94
95 try
96 {
97 IE2eEndpoint Endpoint = (IE2eEndpoint)Types.Instantiate(true, T);
98 if (Endpoint is null)
99 continue;
100
101 if (!Endpoint.SupportsSignatures)
102 continue;
103
104 // TODO: Add Support for PQC.
105
107 continue;
108
109 string Fqn = Endpoint.Namespace + "#" + Endpoint.LocalName;
110 endpoints[Fqn] = EllipticCurveEndpoint;
111 }
112 catch (Exception ex)
113 {
114 Log.Exception(ex);
115 }
116 }
117 }
118
126 public static bool TryGetAlgorithm(string LocalName, string Namespace,
127 out EllipticCurveEndpoint Algorithm)
128 {
129 if (Namespace.StartsWith("urn:ieee:"))
130 Namespace = Namespace.Replace("urn:ieee:", "urn:nf:");
131
132 lock (endpoints)
133 {
134 return endpoints.TryGetValue(Namespace + "#" + LocalName, out Algorithm);
135 }
136 }
137
144 public static bool TryGetAlgorithm(string CurveName, out EllipticCurveEndpoint Algorithm)
145 {
146 // TODO: Add Support for PQC.
147
148 lock (endpoints)
149 {
150 foreach (EllipticCurveEndpoint Endpoint in endpoints.Values)
151 {
152 if (Endpoint.Curve.CurveName == CurveName)
153 {
154 Algorithm = Endpoint;
155 return true;
156 }
157 }
158 }
159
160 Algorithm = null;
161 return false;
162 }
163 }
164}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Represents an HTTP request.
Definition: HttpRequest.cs:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
Abstract base class for Elliptic Curve endpoints.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1454
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:85
Class managing a script expression.
Definition: Expression.cs:41
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
static bool TryGetAlgorithm(string LocalName, string Namespace, out EllipticCurveEndpoint Algorithm)
Tries to get an algorithm given its fully qualified name.
static bool TryGetAlgorithm(string CurveName, out EllipticCurveEndpoint Algorithm)
Tries to get an algorithm given its corresponding curve name.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
GetAlgorithms()
Gets available cryptographic algorithms.
Abstract base class for End-to-End encryption schemes.
Definition: IE2eEndpoint.cs:13
bool SupportsSignatures
If signatures are supported.
string LocalName
Local name of the E2E endpoint
Definition: IE2eEndpoint.cs:22