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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
6using Waher.Events;
11using Waher.Script;
13
15{
20 {
25 : base("Crypto/GetAlgorithms",
26 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
27 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
28 {
29 }
30
31 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetAlgorithms).Namespace + ".JSON.GetAlgorithms.req");
32 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetAlgorithms).Namespace + ".XML.GetAlgorithms.req");
33
42 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
43 {
45
46 List<NamedDictionary<string, object>> Algorithms = new List<NamedDictionary<string, object>>();
47
48 lock (endpoints)
49 {
50 foreach (EllipticCurveEndpoint Endpoint in endpoints.Values)
51 {
52 Algorithms.Add(new NamedDictionary<string, object>("Algorithm", AgentNamespace)
53 {
54 { "localName", Endpoint.LocalName },
55 { "namespace", Endpoint.Namespace },
56 { "securityStrength", Endpoint.SecurityStrength },
57 { "safe", Endpoint.Safe },
58 { "slow", Endpoint.Slow },
59 { "score", Endpoint.Score }
60 });
61 }
62 }
63
64 await Response.Return(new NamedDictionary<string, object>("AlgorithmsResult", AgentNamespace)
65 {
66 { "Algorithms", Algorithms.ToArray() }
67 });
68 }
69
70 private readonly static Dictionary<string, EllipticCurveEndpoint> endpoints = new Dictionary<string, EllipticCurveEndpoint>();
71
72 static GetAlgorithms()
73 {
74 GetEndpointsLocked();
75
76 Types.OnInvalidated += (Sender, e) =>
77 {
78 lock (endpoints)
79 {
80 endpoints.Clear();
81 GetEndpointsLocked();
82 }
83 };
84 }
85
86 private static void GetEndpointsLocked()
87 {
88 foreach (Type T in Types.GetTypesImplementingInterface(typeof(IE2eEndpoint)))
89 {
90 if (T.IsAbstract)
91 continue;
92
93 try
94 {
95 IE2eEndpoint Endpoint = (IE2eEndpoint)Types.Instantiate(true, T);
96 if (Endpoint is null)
97 continue;
98
99 if (!Endpoint.SupportsSignatures || !Endpoint.SupportsSharedSecrets)
100 continue;
101
103 continue;
104
105 string Fqn = Endpoint.Namespace + "#" + Endpoint.LocalName;
106 endpoints[Fqn] = EllipticCurveEndpoint;
107 }
108 catch (Exception ex)
109 {
110 Log.Exception(ex);
111 }
112 }
113 }
114
122 public static bool TryGetAlgorithm(string LocalName, string Namespace,
123 out EllipticCurveEndpoint Algorithm)
124 {
125 if (Namespace.StartsWith("urn:ieee:"))
126 Namespace = Namespace.Replace("urn:ieee:", "urn:nf:");
127
128 lock (endpoints)
129 {
130 return endpoints.TryGetValue(Namespace + "#" + LocalName, out Algorithm);
131 }
132 }
133
140 public static bool TryGetAlgorithm(string CurveName, out EllipticCurveEndpoint Algorithm)
141 {
142 lock (endpoints)
143 {
144 foreach (EllipticCurveEndpoint Endpoint in endpoints.Values)
145 {
146 if (Endpoint.Curve.CurveName == CurveName)
147 {
148 Algorithm = Endpoint;
149 return true;
150 }
151 }
152 }
153
154 Algorithm = null;
155 return false;
156 }
157 }
158}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Represents an HTTP request.
Definition: HttpRequest.cs:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
Abstract base class for Elliptic Curve endpoints.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
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:1353
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:84
Class managing a script expression.
Definition: Expression.cs:39
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:12
bool SupportsSignatures
If signatures are supported.
bool SupportsSharedSecrets
If shared secrets can be calculated from the endpoints keys.
string LocalName
Local name of the E2E endpoint
Definition: IE2eEndpoint.cs:25